【发布时间】:2017-10-01 16:36:52
【问题描述】:
我无法从组件中的一个 Vuex 模块访问 getter,即使我可以在 Vue 开发工具中看到 getter。
我的store.js 文件:
import Vue from 'vue';
import Vuex from 'vuex';
import subsub from './modules/subsub';
Vue.use(Vuex);
export default new Vuex.Store({
state: { },
actions: { },
mutations: { },
getters: { },
modules: {
subsub,
},
});
我的modules/subsub.js 文件:
const state = {
categories: [{
name: 'one',
path: 'two',
...
}, {
name: 'twocat',
path: 'two',
...
}],
};
const actions = { };
const mutations = { };
const getters = {
filterCategories(state) {
return state.categories;
},
filtertwo(state) {
const filteri = state.categories.filter((catee) => {
return catee.name === 'twocat';
});
return filteri;
},
};
export default {
namespaced: true,
state,
actions,
mutations,
getters,
};
我的组件:
<template>
<div> {{ filterCategories }} </div>
</template>
<script>
import { mapGetters } from 'vuex';
export default {
computed: {
...mapGetters([
'categories',
'filtertwo',
'filterCategories',
]),
filtertwo() {
return this.$store.getters.filtertwo;
},
filterCategories() {
return this.$store.getters.filterCategories;
},
},
</script>
那么,我错过了什么?有没有其他语法可以从模块中访问 getter?
【问题讨论】:
-
您没有在控制台中收到任何错误?实例化Vue的时候是不是在store里路过?
-
我在控制台中没有收到任何错误。当我实例化 Vue 时,你的意思是在商店中传递什么? ..顺便说一句,我尝试使用 @click 事件作为 getter 并且甚至没有工作
-
无论你在哪里实例化主应用程序 Vue 组件(可能是
main.js)。您应该导入 vuex 存储并将其包含在通过new Vue({...})传递的配置对象中 -
/* eslint-disable no-new */ new Vue({ el: '#app', router, store, template: '
', components: { App, Icon }, });
标签: javascript vue.js vuejs2 vue-component vuex