从 Vuex 中的一个模块更改另一个模块状态 [英] Change another module state from one module in Vuex
问题描述
我的 vuex 商店中有两个模块.
I have two modules in my vuex store.
var store = new Vuex.Store({
modules: {
loading: loading
posts: posts
}
});
在模块 loading
中,我有一个属性 saving
可以设置 true
或 false
和还有一个名为 TOGGLE_SAVING
的变异函数来设置这个属性.
In the module loading
, I have a property saving
which can be set either true
or false
and also have a mutation function named TOGGLE_SAVING
to set this property.
在模块posts
中,在获取帖子之前和之后,我想更改属性saving
.我是通过从 posts
模块中的一个操作调用 commit('TOGGLE_SAVING')
来实现的.
In the module posts
, before and after fetching posts, I want to change the property saving
. I am doing it by calling commit('TOGGLE_SAVING')
from one of the actions in the posts
module.
var getPosts = function (context) {
contex.commit(TOGGLE_LOADING);
};
当它尝试提交时,我在控制台中出现以下错误
When it tried to commit, I got following error in the console
[vuex] unknown local mutation type: TOGGLE_LOADING, global type: posts/TOGGLE_LOADING
如何使用 commit
在另一个模块中改变状态?
How can I mutate state in another module using commit
?
推荐答案
按照建议使用以下参数尝试 这里;
Try it with following parameters as suggested here;
commit('TOGGLE_LOADING', null, { root: true })
如果您将 namespaced
设置为 true(在 Nuxt 中这是模块模式下的默认设置),则变为:
If you have namespaced
set to true (in Nuxt that's the default when in modules mode), this becomes:
commit('loading/TOGGLE_LOADING', null, { root: true })
这篇关于从 Vuex 中的一个模块更改另一个模块状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!