gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 06:47:36 25 4
gpt4 key购买 nike

我在 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 访问状态或 getter?

最佳答案

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

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

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

关于javascript - 如何在 Nuxt 中从一个 Vuex 状态访问另一个状态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62755962/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com