vue.js - vuex 模块还需要命名空间吗?

标签 vue.js vuejs2 vue-component vuex

我已经建立了一个相当大的包含多个模块的 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/

相关文章:

javascript - VueJS - 我如何只有一个路由路径来加载具有不同 ID 的相同组件

vue.js - 在自定义组件内时,单选按钮不遵守选中状态

javascript - 在 Vue 单文件组件中启用管道运算符的最简单方法

javascript - Vuejs全局注册vuefire

vue.js - 如何在 NuxtJs 路由中使用 *(星号)?

javascript - 自定义输入组件忽略来自方法的更新

javascript - 如何在 vue 组件中重用全局 javascript 文件

javascript - 导入不适用于 Firebase Client v9 (npm)

javascript - Vuejs在悬停时更改图像src

css - 即使描述发生变化,如何设置盒子的高度保持不变? (CSS)