首页 文章

从vuejs组件观察vuex状态更改

提问于
浏览
1

我是vue.js和vuex的新手 . 我有一个组件需要在状态中有特定数据时调度操作 . 我怎样才能做到这一点 .

例:

export default {
   name: 'MyComponent',
   computed: {
      targetItem() {
         return this.$store.getters.getTarget(this.$route.params.id);
      }
   }
}

在上面的示例中,当 targetItem 具有值时,我想在商店上发送新操作 . 这样我就可以通过vuex动作触发ajax请求来收集有关 targetItem 的更多数据

谢谢

1 回答

  • 0

    我最终找到了一个有效的解决方案

    export default {
       name: 'MyComponent',
       computed: {
          targetItem() {
             return this.$store.getters.getTarget(this.$route.params.id);
         }
       },
       watch: {
          shop (newVal, oldVal) {
             if(!!newVal && !oldVal) {
                const {targetItem} = this;           
                this.$store.dispatch('ACTION_NAME',{targetItem});
             }
          }
       }
    }
    

    谢谢

相关问题