简体   繁体   English

vuex 订阅个人变异

[英]vuex subscribe to individual mutation

Is it possible to subscribe to an individual mutation?是否可以订阅单个突变?

Instead of:而不是:

this.$store.subscribe((mutation, state) => {
   if(mutation === 'someMutation'){
       doSomething()
   }
})

I would like something like this:我想要这样的东西:

this.$store.subscribe('someMutation', state => {
    doSomething()
})

How about wrapping the method you have somewhere in Vue prototype?将您拥有的方法包装在 Vue 原型中的某处怎么样?

So instead of having:所以,而不是:

this.$store.subscribe((mutation, state) => {
   if(mutation === 'someMutation'){
       doSomething()
   }
})

You would have something like:你会有类似的东西:

Vue.prototype.subscribeMutation = function(someMutation, someFunction) {
       this.$store.subscribe((mutation, state) => {
       if(mutation === someMutation){
           someFunction(state)
       }
    })
}

I haven't tested the code, but you should be able to get the working result easily.我还没有测试代码,但你应该能够很容易地得到工作结果。

From vuex docs:来自 vuex 文档:

The handler is called after every mutation and receives the mutation descriptor and post-mutation state as arguments.处理程序在每次突变后被调用,并接收突变描述符和突变后状态作为参数。

It will react to all mutations, So you only can implement with your own conditional.它将对所有突变做出反应,因此您只能使用自己的条件来实现。

could you tell please what the reason have subscribtion to the mutation if the only work of mutation is changing store: 如果突变的唯一工作是改变商店,你能告诉我原因是什么原因订阅了突变:

[types.MUTATE_SET_ACTIVE_TICKER]: (state, payload) => {
state.activeTicker = payload
},

and then you can take the data from the store in the component directly: 然后您可以直接从组件中的商店获取数据:

this.$store.state.activeTicker

Try something like this:尝试这样的事情:

this.$store.subscribe(mutation => {
                if (mutation.type === 'setData') {
//  do something here
                }
            });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM