我正在动态注册 vuex 商店模块
store.registerModule('home.grid', GridStore)
然后在组件中:
export default {
name: 'GridComponent',
props: {
namespace: {
type: String,
required: true
},
computed: {
...mapState(this.namespace, ['filter']) // doesn't work
filter() { // more verbose but working
return this.$store.state[this.namespace].filter
}
}
...
Run Code Online (Sandbox Code Playgroud)
但我得到了错误:
无法读取未定义的属性“命名空间”
任何的想法 ?
问题最初是从“gabaum10”这里提出的https://forum.vuejs.org/t/vuex-dynamic-namespaces-in-mapstate-mapactions/28508
在这里解决https://github.com/vuejs/vuex/issues/863#issuecomment-329510765
{
props: ['namespace'],
computed: mapState({
state (state) {
return state[this.namespace]
},
someGetter (state, getters) {
return getters[this.namespace + '/someGetter']
}
}),
methods: {
...mapActions({
someAction (dispatch, payload) {
return dispatch(this.namespace + '/someAction', payload)
}
}),
...mapMutations({
someMutation (commit, payload) {
return commit(this.namespace + '/someMutation', payload)
})
})
}
}
Run Code Online (Sandbox Code Playgroud)