vue.js - 从 vuex 中的模块 getter 访问根状态

标签 vue.js vuex nuxt.js

我的根状态包含来自 nuxt/auth 的身份验证数据..

在 store/modules/messages/内部,我还有状态和 setter/getter 等。

在 getter 内部,我需要从根状态获取身份验证数据,但我不知道如何..

我尝试将 rootState 从模块添加到 index.js:

import state from './state'
import rootState from '../../state'
import * as actions from './actions'
import * as mutations from './mutations'
import * as getters from './getters'

export default {
  namespaced: true,
  state,
  rootState,
  getters,
  mutations,
  actions
}

export const avatar = (rootState) => rootState.auth.user.avatar

但这仍然返回模块状态..

最佳答案

在 vuex 模块中,getter 获取 4 个参数,即本地状态、本地 getter、根状态和根 getter。

// messages/getters.js

export function avatar (state, getters, rootState, rootGetters) {
  return rootState.auth.user.avatar
}

关于vue.js - 从 vuex 中的模块 getter 访问根状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56921964/

相关文章:

javascript - Vuex:附加多个项目以存储在突变中

vue.js - 如何配置运行在 ie 11 上的 Nuxt.js

javascript - 出现此错误 : Cannot read property 'length' of undefined

vue.js - 与路由器链接相比,使用 nuxt-link 有什么优势?

node.js - NPM安装失败

javascript - V-for 单击所有按钮而不是仅单击一个按钮

vue.js - NuxtJS - 无法读取未定义的属性 'title' - 奇怪的行为

javascript - 如何保留用户名登录 vue.js

vue.js - Nuxt.js - 谷歌分析设置不跟踪任何事件

vue.js - 通过迭代创建菜单时出现问题