[javascript] Vuex : 다른 모듈의 액세스 상태

나는 액세스하려는 state.session에서 instance.js에서 records_view.js. 이것은 어떻게 이루어 집니까?

store / modules / instance.js

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

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

store / modules / 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
    );
  }
};

이것은 약간의 추가 컨텍스트를위한 것입니다.

store / 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
  }
});



답변

state로컬 상태를 참조 rootState하며 다른 모듈의 상태에 액세스 할 때 사용해야합니다.

let session = context.rootState.instance.session;

문서 : https://vuex.vuejs.org/en/modules.html


답변

액션에서 :

'contacts:update' ({ commit, rootState }) {
    console.log('rootState', rootState.users.form)
    ......

  },


답변

저에게는 vuex 모듈이 있었지만 다른 파일에서 STATE를 업데이트하려면 돌연변이가 필요했습니다. THIS를 추가하여이를 달성 할 수있었습니다.

모듈에서도 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;
 }
}


답변

제 경우에는 이것이 저에게 효과적이었습니다.

ModuleA.js 파일에서 :

Module A:
export const state = {
    parameterInA: 'A'
 }
export const action = {
    showParameterB() {
    console.log("Parameter B is: " + this.state.B. parameterInB)
}

ModuleB 파일에서 :

export const state = {
    parameterInB: 'B'
 }

export const action = {
    showParameterA() {
    console.log("Parameter A is: " + this.state.A.parameterInA)
}

루트의 index.js에서 ModuleA 및 B를 가져와야합니다.

import * as A from 'ModuleA.js'
import * as B from 'ModuleB.js'

이렇게하면 상태 매개 변수가 하위 모듈에서 상호 참조 될 수 있습니다.


답변

gettersession 에서 액세스하려면 다음과 같이 상태 를 정의해야합니다 .

const state = {
  session: ''
}

이 상태 값을 설정하기 위해 작업에서 호출되는 변형 을 작성해야 합니다.


답변