【问题标题】:Vuex: Access State From Another ModuleVuex:从另一个模块访问状态
【发布时间】:2017-05-12 23:49:47
【问题描述】:

我想从records_view.js 访问instance.js 中的state.session。这是如何实现的?

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

【问题讨论】:

    标签: javascript vue.js vuex


    【解决方案1】:

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

    let session = context.rootState.instance.session;
    

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

    【讨论】:

    • 如果你想直接访问一个getter而不是状态,你也可以做context.rootGetters之类的事情。
    • 虽然正确,但上面的答案并不完整。下面的帖子说明了如何将上面引用的上下文传递到操作中。
    【解决方案2】:

    从一个动作:

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

    【讨论】:

    • 虽然这可能会回答问题,但最好解释一下为什么代码可以与引用一起使用。
    • 对于初学者来说,上面的语法是令人困惑的。最好将上下文作为参数传递,然后通过它访问提交和根状态。速记让人迷失方向。这更容易理解: 'contacts:update' (context) { console.log('rootState', context.rootState.users.form) ...... context.commit....... },
    • @LukeF。 - 这是Vuex documentation 中使用的标准语法,除了一次之外,也是几乎在其他任何地方使用的方式。任何看过动作docs的人都会在那里看到它以及那里给出的解构解释。
    • 确实如此。同时,如果文档清楚,我们中的许多人就不会在这里。 :)
    • 这里还有一个解构的参考/解释,它比 Vue 文档提供的引用更容易理解:courses.bekwam.net/public_tutorials/…
    【解决方案3】:

    对我来说,我有 vuex 模块,但需要一个突变来更新不同文件中的状态。能够通过添加 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;
     }
    }
    

    【讨论】:

    • 有趣。这确实有效。我想知道这是否是我们可以依赖的记录/支持的行为,或者只是一个不错的 hack。目前,我正在使用这个解决方案,因为它不会发出警告并且事情会正常运行。谢谢!
    • 你也可以只用context.commit('user/change_amount', new_amount, {root: true})
    【解决方案4】:

    就我而言,这就是它的工作原理。

    在 ModuleA.js 文件中:

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

    在文件模块B中:

    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'
    

    这样,状态参数可以在子模块中交叉引用。

    【讨论】:

      【解决方案5】:
      this.$store.state.instance.session
      

      其中“instance”是模块名称,“session”是您尝试访问的 Vuex 状态变量。

      【讨论】:

        【解决方案6】:

        假设您有两个模块:ModuleAModuleB

        如果您想从 ModuleB 访问 ModuleA 的 状态,您可以执行以下操作:

        // inside ModuleB (where there is stateA in ModuleA)
        getters: {
                getStateA(state, _, rootState) {
                    return rootState.ModuleA.stateA;
                },
            },
        

        【讨论】:

          【解决方案7】:

          您需要在您的状态中定义session,如下所示,以便在您的getters 中访问它:

          const state = {
            session: ''
          }
          

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

          【讨论】:

          • 我有你在这里提到的一切。我可以从组件中访问会话,但我不确定如何从另一个 Vuex 模块(即 records_view.js)中访问会话。
          • @Donnie 试试context.rootState.instance.session
          猜你喜欢
          • 1970-01-01
          • 2017-07-25
          • 2018-02-10
          • 1970-01-01
          • 1970-01-01
          • 2021-05-15
          • 2019-11-17
          • 2021-08-27
          • 2020-10-26
          相关资源
          最近更新 更多