vuex unknown action type

clipboard.png

clipboard.png

clipboard.png

clipboard.png

我把index中命名空间去掉了 好用了 但是不知道具体原因

clipboard.png
是上面这样么 还是不好使

阅读 8.6k
2 个回答

如果加上命名空间 你试下 ./getmessage
就像分包之后 指向action 要加上包路径一样
vuex文档

const store = new Vuex.Store({
  modules: {
    account: {
      namespaced: true,

      // 模块内容(module assets)
      state: { ... }, // 模块内的状态已经是嵌套的了,使用 `namespaced` 属性不会对其产生影响
      getters: {
        isAdmin () { ... } // -> getters['account/isAdmin']
      },
      actions: {
        login () { ... } // -> dispatch('account/login')
      },
      mutations: {
        login () { ... } // -> commit('account/login')
      },

      // 嵌套模块
      modules: {
        // 继承父模块的命名空间
        myPage: {
          state: { ... },
          getters: {
            profile () { ... } // -> getters['account/profile']
          }
        },

        // 进一步嵌套命名空间
        posts: {
          namespaced: true,

          state: { ... },
          getters: {
            popular () { ... } // -> getters['account/posts/popular']
          }
        }
      }
    }
  }
})

加了命名空间,就有了命名空间的嵌套了,一级的不需要加命名空间

命名空间模式 mapActions第一个参数需要传模块的模块名

如下示例:

export default {
    methods: {
        ...mapActions('moduleKey', ['foo', 'bar'])
    }
}

moduleKey的值为你在定义modules时候对应对象的key 比如下面的 foobar

const store = new Vuex.Store({
    modules: {
        foo: moduleFoo,
        bar: moduleBar
    }
})

参考 官方文档

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题