如何在Nuxt中从另一个状态访问一个Vuex状态?

时间:2020-07-06 12:20:36

标签: javascript vue.js vuex nuxt.js

我在Nuxt store目录中有两个Vuex存储。即使通过routeState,我也无法从另一个商店访问一个商店的状态。

商店1:index.js

export const state = () => ({
    token: false
})
export const getters = {}
export const mutations = {}
export const actions = {}

商店2:api.js

export const state = () => ({
    apiBase: 'https://myurl.com/'
})
export const getters = {
    getAPI: (state, rootState) => {
        // Need the state token from index.js here
    },
}
export const mutations = {}
export const actions = {}

在这里

  • state返回api.js中的状态变量,即apiBase
  • routeState返回api.js中的吸气剂
  • this在Vuex吸气剂中未定义,不起作用

如何从index.js访问状态或获取方法?

1 个答案:

答案 0 :(得分:2)

rootState在第三个参数中公开给吸气剂。

const getters = {
  getApi: (state, getters, rootState) => {
    // Access the state token from index.js
    rootState.token
 }}

请查看此页面以获取更多详细信息,Vuex Docs