【发布时间】:2020-08-16 20:16:35
【问题描述】:
所以我迷路了...
我有一个 Vuex。我在文件common.js 中有一个模块common:
const initState = {
fruits: []
}
export default {
state: initState,
mutations: {
SET_FRUITS(state, fruits) {
console.log(fruits);
state.fruits = fruits;
}
},
actions: {
async getFruits({ commit }) {
const fruits =[
{
text: 'Apple',
value: {id: 1, val: 'apple'},
},
{
text: 'Banana',
value: {id: 2, val: 'banana'},
},
{
text: 'Pear',
value: {id: 3, val: 'pear'},
},
{
text: 'Plum',
value: {id: 4, val: 'plum'},
}
];
commit('SET_FRUITS', fruits);
}
}
}
我的store 在index.js:
import Vue from 'vue';
import Vuex from 'vuex';
import common from './modules/common';
Vue.use(Vuex);
export default new Vuex.Store({
modules: {
common,
}
})
我尝试在 App.vue 中访问 state.fruits:
<template>
<div>
{{ this.$store.state.fruits }}
</div>
</template>
<script>
export default {
name: 'App',
components: {
},
async mounted() {
await this.$store.dispatch('getFruits');
console.log(this.$store.state.fruits);
},
}
</script>
console.log from mutation 返回水果数组。有用。这个很酷。
App.vue 中来自mounted() 的console.log 返回未定义。这不酷。
为什么?我错过了什么?
【问题讨论】:
-
你试过
this.$store.state.comon.fruits吗? -
嗯,我不太确定,这就是为什么它是一个单行问题。但是带来的问题在于这个documentation
标签: javascript vue.js vuex