【问题标题】:Vuex - access state in modulesVuex - 模块中的访问状态
【发布时间】: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);
    }
  }
}

我的storeindex.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


【解决方案1】:

这是一个工作示例。还要仔细检查你的 vuex 存储配置/注册,不要忘记使用dispatch

const store = new Vuex.Store({
  state: {
    fruits: []
  },
  actions: {
    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'
          },
        }
      ];

      console.log(fruits)

      commit('SET_FRUITS', fruits);
    }
  },
  mutations: {
    SET_FRUITS(state, fruits) {
      state.fruits = fruits;
    }
  },
});

new Vue({
  el: "#app",
  store,
  computed: {
    fruits() {
      return this.$store.state.fruits;
    },
  },
  mounted() {
    this.$store.dispatch('getFruits')
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vuex/3.5.1/vuex.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id='app'>
  <div v-for='(fruit, i) in fruits' :key="i">
    {{fruit}}
  </div>
</div>

【讨论】:

  • 看来你改变了我的做法。我询问了对模块中状态的访问,我提交的代码是一个非常简化的问题示例。埃尔达的答案是我需要的答案。当然,您的解决方案很好,但与我的问题不符:)
猜你喜欢
  • 2021-08-27
  • 2019-11-17
  • 2017-05-12
  • 2019-03-22
  • 2017-12-05
  • 2021-05-15
  • 2018-05-19
  • 2019-03-22
  • 1970-01-01
相关资源
最近更新 更多