Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/gwt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vuejs2 Nuxt&x2B;Vuex映射器值始终未定义_Vuejs2_Vuex_Nuxt.js - Fatal编程技术网

Vuejs2 Nuxt&x2B;Vuex映射器值始终未定义

Vuejs2 Nuxt&x2B;Vuex映射器值始终未定义,vuejs2,vuex,nuxt.js,Vuejs2,Vuex,Nuxt.js,我正在将VueX与Nuxt.JS一起使用,因此让我们假设文件store/search.JS中有以下代码: export const state = () => ({ results: null }); export const mutations = { setResults(state, { results }) { state.results = results; } }; export const actions = { s

我正在将VueX与Nuxt.JS一起使用,因此让我们假设文件
store/search.JS
中有以下代码:

    export const state = () => ({
    results: null
});

export const mutations = {
    setResults(state, { results }) {
        state.results = results;
    }
};

export const actions = {
    startSearch({ commit, dispatch }, { type, filters }) {
        commit("setResults", { type, filters });
    }
};

export const getters = {
    results: state => state.results
};
现在,在我的组件
results.vue
中,在computed属性下,我有如下内容:

<template>
<button @click="handleSearch">Search</button>
<div v-if="results && results.length" class="results" >
<div v-for="item in results" :key="item.id">

          {{item}}

        </div>
</div>
</template>

<script>

import { mapActions, mapGetters } from "vuex";
 data() {
    return {
      selected_type: null,
      filters: null
    };
  },
methods: {
    setType(type) {
      this.selected_type = type;
      this.handleSearch();
    },
    setFilters(filters) {
      this.filters = filters;
    },
    handleSearch() {
      this.startSearch({ type: this.selected_type, filters: this.filters });
    },
    ...mapActions("search", {
      startSearch: "startSearch"
    })
  },
computed: {
...mapGetters("search", {
      results: "results"
    })
}
</script>

搜寻
{{item}}
从“vuex”导入{mapActions,mapGetters};
数据(){
返回{
所选类型:空,
筛选器:空
};
},
方法:{
设置类型(类型){
此。所选类型=类型;
这个。handleSearch();
},
设置过滤器(过滤器){
this.filters=过滤器;
},
handleSearch(){
this.startSearch({type:this.selected_type,filters:this.filters});
},
…映射操作(“搜索”{
startSearch:“startSearch”
})
},
计算:{
…映射器(“搜索”{
结果:“结果”
})
}
我的问题是:为什么for循环(模板部分)中的
项总是返回未定义的

非常感谢您的回答。

到目前为止,我发现: 在computed中,应该是数组,而不是对象,因此:

...mapGetters("search", [
      "results"
]

// Now results is populated.