2017-08-11 54 views
1

我是vue.js和vuex的新手。当一个特定的数据在状态中可用时,我有一个组件需要分派一个动作。我怎样才能做到这一点。从vuejs组件观看vuex状态变化

实施例:

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

在上述我想分派在商店一个新的动作时targetItem具有值的例子。这是这样我就可以触发通过vuex动作一个Ajax请求收集更多的数据约targetItem

感谢

+0

什么都有,你已经试过,有什么不符合你的预期? – thanksd

+0

你的用例对我来说很不清楚。你是否在寻找类似于[小提琴](https://jsfiddle.net/awolf2904/0txhcvae/)的东西?请在您的问题中添加更多详细信息。你打算怎么处理这个行为?它发送一个Ajax请求,它是否改变状态等? – AWolf

回答

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}); 
     } 
     } 
    } 
} 

感谢解决