Vuex:来自其他模块的访问状态

我要访问state.sessioninstance.jsrecords_view.js这是如何完成的?

商店/模块/instance.js

const state = {
  // This is what I want to access in records_view.js
  session: {}
};

const getters = {
  sessionGetter: state => state.session
};

商店/模块/records_view.js

const actions = {
  getSettingsAction (context, props) {
    // This is how I'm trying to access session, which doesn't work
    let session = context.state.instance.session;

    Api(
      context,
      {
        noun: props.noun,
        verb: 'GetRecordsViewSettings',
        orgUnitKey: _.has(session, 'orgunit.key') ? session.orgunit.key : '',
        data: {}
      },
      props.callback
    );
  }
};

这是为了增加一些上下文。

商店/index.js

import Vue from 'vue';
import Vuex from 'vuex';
import * as actions from './actions';
import * as getters from './getters';
import * as types from './mutation-types';

import instance from './modules/instance';
import recordsView from './modules/records_view';

Vue.use(Vuex);

export default new Vuex.Store({
  state,
  actions,
  getters,
  mutations,
  modules: {
    instance,
    recordsView
  }
});
神无凯2020/03/11 10:21:29

对我来说,我有vuex模块,但需要进行更改以更新其他文件中的STATE。能够通过添加此来完成此操作

即使在模块中,您也可以通过console.log(this.state)查看您可以访问的全局状态

const mutations = {
MuteChangeAmt(state, payload) {
//From user module; Need THIS keyword to access global state
this.state.user.payees.amount = payload.changedAmt;
 }
}
理查德Itachi2020/03/11 10:21:29

您需要session像下面这样在状态中定义,才能在getter中访问它

const state = {
  session: ''
}

您必须编写一个mutation,从您的操作中将其调用以设置此状态值。

Green小胖2020/03/11 10:21:29

state引用本地状态,并rootState在访问其他模块的状态时使用。

let session = context.rootState.instance.session;

文档:https : //vuex.vuejs.org/en/modules.html