【问题标题】:How to access one Vuex state from another in Nuxt?如何在 Nuxt 中从另一个访问一个 Vuex 状态?
【发布时间】:2020-10-26 13:43:34
【问题描述】:

我在 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 中的 getter
  • this 在 Vuex getter 中未定义,它不起作用

如何从 index.js 访问 state 或 getter?

【问题讨论】:

    标签: javascript vue.js vuex nuxt.js


    【解决方案1】:

    rootState 在第三个参数中暴露在一个 getter 中。

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

    查看此页面了解更多详情Vuex Docs

    【讨论】:

      猜你喜欢
      • 2017-05-12
      • 2021-11-07
      • 2021-04-21
      • 2017-03-07
      • 2018-02-10
      • 1970-01-01
      • 1970-01-01
      • 2018-01-03
      • 1970-01-01
      相关资源
      最近更新 更多