我如何从Vuex的另一家商店打电话给吸气剂?

时间:2018-07-31 00:40:26

标签: vue.js vuejs2 vuex

我有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获取recordType值

1 个答案:

答案 0 :(得分:1)

您可以使用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']
    });
  }
};
相关问题