在我看到的几乎所有关于 vuex 模块注册的指南、教程、帖子等中,如果模块是由组件注册的,则 createNamespacedHelpers
会在 export 之前导入和定义default
组件语句,例如:
import {createNamespacedHelpers} from 'vuex'
const {mapState} = createNamespacedHelpers('mymod')
import module from '@/store/modules/mymod'
export default {
beforeCreated() {
this.$store.registerModule('mymod', module)
}
}
这按预期工作,但如果我们希望模块具有唯一或用户定义的命名空间怎么办?
import {createNamespacedHelpers} from 'vuex'
import module from '@/store/modules/mymod'
export default {
props: { namespace: 'mymod' },
beforeCreated() {
const ns = this.$options.propData.namespace
this.$store.registerModule(ns, module)
const {mapState} = createNamespacedHelpers(ns)
this.$options.computed = {
...mapState(['testVar'])
}
}
}
我以为这行得通,但行不通。
为什么需要这样的东西? 因为
export default {
...
computed: {
...mapState(this.namespace, ['testVar']),
...
},
...
}
不工作
最佳答案
这种通过使用 beforeCreate 来访问你想要的变量的工作方式应该可以工作,我是根据传递到你的组件实例中的 props 做的:
import { createNamespacedHelpers } from "vuex";
import module from '@/store/modules/mymod';
export default {
name: "someComponent",
props: ['namespace'],
beforeCreate() {
let namespace = this.$options.propsData.namespace;
const { mapActions, mapState } = createNamespacedHelpers(namespace);
// register your module first
this.$store.registerModule(namespace, module);
// now that createNamespacedHelpers can use props we can now use neater mapping
this.$options.computed = {
...mapState({
name: state => state.name,
description: state => state.description
}),
// because we use spread operator above we can still add component specifics
aFunctionComputed(){ return this.name + "functions";},
anArrowComputed: () => `${this.name}arrows`,
};
// set up your method bindings via the $options variable
this.$options.methods = {
...mapActions(["initialiseModuleData"])
};
},
created() {
// call your actions passing your payloads in the first param if you need
this.initialiseModuleData({ id: 123, name: "Tom" });
}
}
我个人在我正在导入的模块中使用了一个辅助函数来获取命名空间,所以如果我有我的模块存储项目并使用路由器将 123 的 projectId
传递到我的组件/页面和/或 Prop 看起来像这样:
import projectModule from '@/store/project.module';
export default{
props['projectId'], // eg. 123
...
beforeCreate() {
// dynamic namespace built using whatever module you want:
let namespace = projectModule.buildNamespace(this.$options.propsData.projectId); // 'project:123'
// ... everything else as above
}
}
希望你觉得这很有用。
关于vue.js - Vuex:具有动态命名空间的 createNamespacedHelpers,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54593910/