代码之家  ›  专栏  ›  技术社区  ›  Learner

从vuex中的模块getters访问根状态

  •  0
  • Learner  · 技术社区  · 5 年前

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

    我试着把rootState添加到索引.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
    

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

    1 回复  |  直到 5 年前
        1
  •  11
  •   Sumurai8    5 年前

    在vuex模块中,getter有4个参数,即local state、local getter、root state和root getter。

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