我已经建立了一个相当大的包含多个模块的 Vuex 项目。
https://vuex.vuejs.org/en/modules.html
要从 search
模块中获取 getter 的示例,可以这样处理:
computed: {
filters() {
return this.$store.state.search.filters;
}
}
因为我需要通过引用我的属性链中的 search
属性来访问模块的状态,我还需要为我的模块命名空间吗?
文档说明如下:
By default, actions, mutations and getters inside modules are still registered under the global namespace - this allows multiple modules to react to the same mutation/action type.
https://vuex.vuejs.org/en/modules.html#namespacing
但是,如果模块在商店中属于其自己的属性,这并不是模块本身之间可能发生的唯一冲突,可以通过简单的文件命名约定轻松避免这种情况?
我在这里错过了什么?
最佳答案
But if the module is under its own property in the store isn't the only conflict that could happen between modules themselves, which can easily be prevented by a simple naming convention of the files?
不对,你误会了。状态本身是正确命名空间的,但是突变、 Action 和 getter 仍然在全局级别上收集,因此您可以,例如分派(dispatch)一个 Action,来自不同模块的多个 Action 对其作出 react 。
这是默认行为,但好消息是,有一个命名空间突变、 Action 和 getter 的选项:“namespaced: true”。
它记录在这里:https://vuex.vuejs.org/en/modules.html#
向下滚动到“命名空间”部分。
关于vue.js - vuex 模块还需要命名空间吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44886716/