简体   繁体   中英

Setting a variable on vuex module from another module getters Nuxt.js

The application is build with Nuxt.js. I am trying to initiate a variable on vuex module which is used to call axios on each actions.

store/program.js

let program_url = 'programs/';

export const actions = {
  async programList({commit}) {
    await this.$axios.$get(program_url).then((response) => {
      commit("ALL_PROGRAMS", response);
    });
  },

The problem I am facing is that this variable depends on a state variable in another vuex module. What I am trying to build here is on store/program.js I want to initiate a variable called program = <dynamic_id_from_another_vuex_module>/program

The other store file is store/university.js

export const state = () => ({
  settings: [],
  id: null
});

export const getters = {
  getId(state) {
    return state.id;
  }
};

So How do I do something like below inside my store/program.js ?

let program = store.getters['university/getId'] + 'program';

You need to use rootState in your getters like following...

getId(state, getters, rootState) {
    return rootState.university.id  // Here I assume university is the another  module
}

That's it :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM