不能在没有突变的情况下发送动作
Cant dispatch action without mutation
我无法在不提交突变的情况下使用 vuex 发送操作。
我像往常一样导入了常用的 mapActions 等(切记所有工作正常,我可以执行突变、getter,通过 mapMutations、mapActions 等获取我的状态,如果我包含提交,我什至可以调度操作).
对于最简单的事情,我在专用 store/actions.js.
中创建了一个动作
repeatWord(data) {
console.log(data)
}
在我的 vue 组件上,我将其定义如下:
methods: {
...mapActions({
repeatWord: 'repeatWord'
)}
我通过以下方式调用它:
this.repeatWord('test')
应该可以,对吧?好吧,它没有。
我在控制台中得到的是 {getters: {...}, state: {...}, rootGetters: {...}, dispatch: f, commit: f,...}
我在这里错过了什么?
来自docs:
Action handlers receive a context object which exposes the same set of methods/properties on the store instance
这是第一个参数。第二个参数是您传递的数据。所以像这样定义你的动作:
repeatWord(context, data) {
console.log(data)
}
您经常会看到 context
参数被解构为:
repeatWord({ commit, dispatch }, data) {
console.log(data)
}
我无法在不提交突变的情况下使用 vuex 发送操作。
我像往常一样导入了常用的 mapActions 等(切记所有工作正常,我可以执行突变、getter,通过 mapMutations、mapActions 等获取我的状态,如果我包含提交,我什至可以调度操作).
对于最简单的事情,我在专用 store/actions.js.
中创建了一个动作repeatWord(data) {
console.log(data)
}
在我的 vue 组件上,我将其定义如下:
methods: {
...mapActions({
repeatWord: 'repeatWord'
)}
我通过以下方式调用它:
this.repeatWord('test')
应该可以,对吧?好吧,它没有。 我在控制台中得到的是 {getters: {...}, state: {...}, rootGetters: {...}, dispatch: f, commit: f,...}
我在这里错过了什么?
来自docs:
Action handlers receive a context object which exposes the same set of methods/properties on the store instance
这是第一个参数。第二个参数是您传递的数据。所以像这样定义你的动作:
repeatWord(context, data) {
console.log(data)
}
您经常会看到 context
参数被解构为:
repeatWord({ commit, dispatch }, data) {
console.log(data)
}