Vuex: access status from another module

I want to access state.session in instance.js from records_view.js . How is this achieved?

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

This is for a bit of added context.

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 } }); 
+52
source share
4 answers

state refers to the local state and rootState should be used when accessing the state of other modules.

 let session = context.rootState.instance.session; 

Documentation: https://vuex.vuejs.org/en/modules.html

+74
source

from action:

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

For me, I had vuex modules, but I needed a mutation to update STATE in another file. Was able to do this by adding THIS

Even in the module, you can see what global state you have access through 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; } } 
0
source

You need to define session in your state, as shown below, to access it in getters :

 const state = { session: '' } 

You need to write a mutation that will be called from your actions to set this state value.

-one
source

Source: https://habr.com/ru/post/1013581/


All Articles