首页 > 解决方案 > 如何从 Vuex 的另一家商店调用 getter?

问题描述

我有vuex一家商店abc.js

import Vue from 'vue';

const state = {
  recordType: null
};

const getters = {

  recordType: state => state.recordType,

};
.... other code

我有其他vuex商店xyz.js

import { API } from '@/api';
import Vue from 'vue';

const state = {
  messages: null
};

const getters = {
  messages: state => state.messages || [],
};

const actions = {
  async openRecord({ dispatch, rootState, commit, state }, record) {

    // api data
    const response = await API.post('api/openrecord/', {
      recordUid: record.recordUid,

      //**** HELP !!! HERE ****//
      recordType: // *** HERE I need to get the recordType getter from abc.js

    });

  }

};

所以在xyz.js商店我需要从abc.js

标签: vue.jsvuejs2vuex

解决方案


您可以使用 rootGetters 属性以及指定命名空间:

rootGetters['abc/recordType']

在您的情况下,它给出:

const actions = {
  async openRecord({ dispatch, rootGetters, commit, state }, record) {
    // api data
    const response = await API.post('api/openrecord/', {
      recordUid: record.recordUid,
      recordType: rootGetters['abc/recordType']
    });
  }
};

推荐阅读