VAAA
VAAA

Reputation: 15039

How do I call a getter from another store in Vuex?

I have vuex a store abc.js:

import Vue from 'vue';

const state = {
  recordType: null
};

const getters = {

  recordType: state => state.recordType,

};
.... other code

The I have other vuex store 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

    });

  }

};

So on xyz.js store I need to get the recordType value from abc.js

Upvotes: 4

Views: 7059

Answers (1)

Pierre Gerbaud
Pierre Gerbaud

Reputation: 131

You can use the rootGetters property, along with specifying namespace :

rootGetters['abc/recordType']

In your case, it gives :

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

Upvotes: 5

Related Questions