2016-10-03 39 views
4

我有以下状态vuex店:发出事件时的状态变化vuex存储特定一块

state: { 
    authed: false 
    ,id: false 
} 

我想看更改到authed状态,并发送一个AJAX组件内呼叫服务器。它需要在各种组件中完成。

我尝试使用store.watch(),但是当idauthed发生更改时会触发。我还注意到,它不同于vm.$watch,因为您无法指定属性。当我试图做到这一点:

store.watch('authed',function(newValue,oldValue){ 
    //some code 
}); 

我得到这个错误:

[vuex] store.watch only accepts a function.

任何帮助表示赞赏!

回答

11

在你的组件只需设置一个getter是否authed状态,看当地的getter:

watch: { 
    'authed': function() { 
    ... 
    } 
} 
+0

谢谢!我不知道为什么我没有想到这一点! – andrei

4

或者你可以使用...

let suscribe = store.subscribe((mutation, state) => { 
    console.log(mutation.type) 
    console.log(mutation.payload) 
}) 
// call suscribe() for unsuscribe 

https://vuex.vuejs.org/en/api.html