简体   繁体   English

Vuex映射状态未定义状态

[英]Vuex map state undefined state

I'm trying to use my state here to pass through as a search query but when pulling the state down using map state it's returning 'undefined' ... I've never had this problem before. 我试图在这里使用我的状态作为搜索查询传递但是当使用map状态向下拉状态时它返回'undefined'...我之前从未遇到过这个问题。

Here's the code: 这是代码:

import Vue from 'vue'
import Hero from '../components/Hero/Hero'
import PopularDest from '../components/PopularDest/PopularDest'

import { mapActions, mapState } from 'vuex'

export default Vue.extend({
template: `
    <div class="page--sport">
        <hero :action="getSportData" page="sport" title="Sport Events"></hero>
        <div class="page--sport__bottom">
            <h2>Popular Sport Events</h2>
            <popular-dest></popular-dest>
        </div>
    </div>
`,
data () {
    return {
        searchQuery: {
            query: [(this as any).searchInput],
            genre: 'sport'
        }
    }
},
updated () {
   console.log(this.searchInput)
},  
components: {
    Hero,
    PopularDest
},
methods: {
    getSportData (): void {
        [(this as any ).getEventData(this.searchQuery)]
    },
    ...mapActions([
        'getEventData'
    ])
},
computed: {
    ...mapState([
        'searchInput'
    ])
}
})

I'm using Vuex modules for the first time in this project which seems to be the only indicator to a problem for me. 我在这个项目中第一次使用Vuex模块,这似乎是我遇到问题的唯一指标。

If you are using Module based Store structure apparently you cannot directly access state of a module inside mapState like that. 如果您显然使用基于模块的存储结构,则无法直接访问mapState中的模块状态。 For example, if you do - this.$store.state.searchInput you will get undefined but if you do this.$store.state.yourModuleName.searchInput you will get the actual value inside the state of that particular module. 例如,如果你这样做 - this.$store.state.searchInput你将得到undefined但是如果你这样做this.$store.state.yourModuleName.searchInput你将获得该特定模块状态内的实际值。

You have two ways to fix this: 您有两种方法可以解决此问题:

1. Property based access within mapState 1. mapState中基于属性的访问

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

2. Using Namespaces in your Module 2. 在模块中使用命名空间

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

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

There's an open issue on this case in Vuex's github page - https://github.com/vuejs/vuex/issues/459 在Vuex的github页面中有一个关于这个案例的公开问题 - https://github.com/vuejs/vuex/issues/459

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM