vue.js - 如何从 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
解决方案
您可以使用 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']
});
}
};
推荐阅读
- tree - 有效的树遍历基本案例和指针
- python - 仅使用 NumPy 优化阶跃函数的代码
- javascript - 使用 $ 或 sequelize 的多个 where
- python - Django Unhasable Type List 即使我传入了一个对象?(多对多字段)
- javascript - 如何调用 Mongoose Find 函数将所有文档作为数组获取?
- docker - Dockerfile keytool:获取“证书别名
已经存在”,即使使用“keytool - 删除” - python - python dialogflow_v2 非常慢
- c++ - 对具有动态分配内存和析构函数的类对象的引用
- python - pygame世界生成算法
- search - 在目录和子目录中搜索/查找所有 .mkv