2

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

store
  modules
    User.js
 index.js

存储/模块/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
}

存储/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

在一个组件中,我需要使用来自用户模块和 index.js 的 ..mapGetters

...mapGetters('User', ['user'], ['sideNav', 'showAssociation']),

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

 Property or method "showAssociation" is not defined

我应该如何写我的 ...mapGetters ?还是我的用户模块中有什么问题?

感谢您的反馈

4

1 回答 1

3

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

computed: {
  ...mapGetters('User', ['user']),
  ...mapGetters(['showAssociation', 'sideNav'])
}
于 2018-07-14T18:21:05.933 回答