简体   繁体   English

vuex:未知的getter:user

[英]vuex: unknown getter: user

I'm experimenting with vuex and I was looking for best way to organize my vuex files I finished with something like this: 我正在尝试使用vuex,我正在寻找组织我的vuex文件的最佳方法,我完成了这样的事情:

/src/store/user/state.js: /src/store/user/state.js:

export default {
  state: {
    user: null
  }
}

/src/store/user/getters.js: /src/store/user/getters.js:

export default {
  getters: {
    user (state) {
      return state.user
    }
  }
}

/src/store/user/mutations.js: /src/store/user/mutations.js:

export default {
  mutations: {
    'SET_USER' (state, user) {
      state.user = user
    }
  }
}

/src/store/user/actions.js /src/store/user/actions.js

export default {
  actions: {
    loginUser ({ commit }, params) {
       commit('SET_USER', {id: 1})
    }
  }
}

/src/store/user/index.js /src/store/user/index.js

import state from './state'
import getters from './getters'
import actions from './actions'
import mutations from './mutations'

export default {
  state,
  getters,
  actions,
  mutations
}

/src/store/index.js: /src/store/index.js:

import Vue from 'vue'
import Vuex from 'vuex'
import user from './user'

Vue.use(Vuex)

export default new Vuex.Store({
  modules: {
    user
  }
})

When I load my code it returns in console following error: 当我加载我的代码时,它会在控制台中返回以下错误:

vuex: unknown getter: user

Each of your user-related files are using export default , which means when you import those files, you are naming the entire object being exported state , getters , etc. 每个与用户相关的文件都使用export default ,这意味着在导入这些文件时,您将命名整个正在导出的对象stategetters等。

So, in the scope of index , the state variable has a property named state , the getters variable has a property named getters , etc. And this is throwing things off. 因此,在index的范围内, state变量有一个名为state的属性, getters变量有一个名为getters的属性,等等。这就是抛弃它。

You should export a const for each of these files instead: 您应该为每个文件导出一个const

export const state = {
  user: null,
}

And then when importing grab the named const like so: 然后在导入时抓取指定的const如下所示:

import { state } from './state'

Alternatively, you could just remove the properties for state , getters , etc. from each file: 或者,您可以从每个文件中删除stategetters等属性:

// state.js
export default {
  user: null,
}

And then import like you're doing already: 然后导入就像你已经做的那样:

import state from './state' 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM