Vuex模块-无法正确设置..mapGetters

时间:2018-07-14 17:22:46

标签: vue.js vuex vuex-modules

我目前的商店结构定义如下

store
  modules
    User.js
 index.js

store / modules / User.js

import * as types from '@/store/mutation_types'
import firebase from 'firebase'

const state = {
  ...
  user: JSON.parse(localStorage.getItem('user')) || null
}

const actions = {
  setUser ({commit}, newUser) {
    console.log('STORE ACTION setUser: ', newUser)
    commit(types.SET_USER, newUser)
  },
  signUserIn ({commit}, payload) {
    ...
  },
  signUserUp ({commit}, payload) {
    ...
  },
  logout: context => {
   ...
  }
}

const mutations = {
 ...
}

const getters = {
  ...
  user: state => state.user
}

export const User = {
  namespaced: true,
  state,
  actions,
  mutations,
  getters
}

store / index.js

/**
 * Import Dependency
 */
import Vue from 'vue'
import Vuex from 'vuex'
import { firebaseMutations } from 'vuexfire'
import * as types from '@/store/mutation_types'
import { User } from '@/store/modules/User.js'
import { Account } from '@/store/modules/Account.js'

Vue.use(Vuex)

const store = new Vuex.Store({
  strict: process.env.NODE_ENV !== 'production',
  state: {
    language: localStorage.getItem('language') || '',
    ...
  },
  actions: {
    switchLanguage ({ commit }, context) {
      commit(types.SET_LANGUAGE, context)
    },
    ...
  },
  getters: {
    language: state => state.language,
    sideNav: state => state.sideNav,
    showAssociation: state => state.showAssociation,
    ...
  },
  mutations: {
    ...
    },
    ...firebaseMutations
  },
  modules: {
    User,
    Account
  }
})

export default store

在组件中,我需要使用User模块和index.js中的..mapGetters

... mapGetters('用户',['用户'],['sideNav','showAssociation']),

这是不对的...我得到一个错误:

 Property or method "showAssociation" is not defined

我应该如何写我的... mapGetters? 还是我的用户模块中有东西?

感谢您的反馈

1 个答案:

答案 0 :(得分:1)

如果您需要从不同的模块中获取吸气剂,只需将它们分别映射:

computed: {
  ...mapGetters('User', ['user']),
  ...mapGetters(['showAssociation', 'sideNav'])
}