我正在尝试从 getters 部分访问 this.$store.commit 并且我看到未定义的错误属性“$store”,我能够从突变访问 this.$store 那么为什么不从 getter 访问呢?
我的 store.js 文件
import Vue from 'vue';
import Vuex from 'vuex';
import order from './modules/order/order'
import dialogList from './modules/dialog/dialogList'
Vue.use(Vuex);
export const store = new Vuex.Store({
state: { },
getters: {
},
modules: {
order,
dialogList
}
});
我的 order.js 文件从 dialogList 调用突变
const state = {
order: {
...
}
}
const getters = {
showInfoDialogWithCalculatedData(state) {
//... some actions
//here this.$store is undefined
this.$store.commit('showInfoDialog', { message: 'test', type: 'error'});
return someData;
}
}
const mutations = {
showInfoDialogWithCalculatedData(state) {
//... some actions
//here this.$store is defined
this.$store.commit('showInfoDialog', { message: 'test', type: 'error'});
}
}
export default {
state,
getters,
}
为什么 this.$store 在 getter 中是未定义的?
答案 0 :(得分:2)
因为getter不应该提交变异,也许你可以设置一个watcher,所以当getter调用更新时你可以提交或者如果你真的想做您可以导入商店并从导入的商店调用提交。
import store from "../index";
const getters = {
showInfoDialogWithCalculatedData(state) {
//... some actions
//Store imported
store .commit('showInfoDialog', { message: 'test', type: 'error'});
return someData;
}