Vuex映射状态未定义状态

时间:2018-03-01 13:56:44

标签: javascript vue.js vuex

我试图在此处使用我的状态作为搜索查询进行传递,但是当使用地图状态将状态拉下来时,它会返回“未定义”状态' ......我之前从未遇到过这个问题。

以下是代码:

$ant build

我在这个项目中第一次使用Vuex模块,这似乎是我遇到问题的唯一指标。

1 个答案:

答案 0 :(得分:3)

如果您正在使用基于模块的存储结构,那么您无法直接访问mapState中的模块状态。例如,如果你这样做 - this.$store.state.searchInput,你将获得undefined,但如果你this.$store.state.yourModuleName.searchInput,你将获得该特定模块状态内的实际值。

您有两种解决方法:

1. mapState中基于属性的访问

...mapState({
    searchInput: state => state.yourModuleName.searchInput,
  })

2. 在模块中使用命名空间

..........
modules: {
yourModuleName: {
  namespaced: true,
.........

/* Using the namespace in mapState */
...mapState('yourModuleName',[
  'searchInput',
])

在Vuex的github页面中有一个关于此案例的公开问题 - https://github.com/vuejs/vuex/issues/459

相关问题