Vuex-绑定帮助器中的动态名称空间(mapState,.) [英] Vuex - dynamic namespaces in binding helpers (mapState, ...)

查看:17
本文介绍了Vuex-绑定帮助器中的动态名称空间(mapState,.)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在动态注册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
    }
 }
 ...

但我收到错误:

无法读取未定义的属性"命名空间"

有什么想法吗?

此处"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)
      })
    })
  }
}

这篇关于Vuex-绑定帮助器中的动态名称空间(mapState,.)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆