为什么Vuex的state在调用的时候要加上模块名但是getter就不需要?

这样搞的很混乱啊,一会儿是this.$store.state.模块.状态名,一会儿是this.$store.getter.获取器名。

有啥办法能够让getter也像状态一样必须按模块调用吗?

阅读 3k
2 个回答

vuex模块内部的 action、mutation、和 getter 默认是注册在全局命名空间

中文的文档没更新,英文文档已经介绍了namspaced属性了,应该是从2.1.0版本开始支持

这个issue又介绍了以后的namespace属性

getter写在模块里面

const moduleA = {
  state: { ... },
  mutations: { ... },
  actions: { ... },
  getters: { ... }
}

const moduleB = {
  state: { ... },
  mutations: { ... },
  actions: { ... }
}

const store = new Vuex.Store({
  modules: {
    a: moduleA,
    b: moduleB
  }
})

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