首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Vuex对象未定义和"[vuex]未知突变类型:“

Vuex对象未定义和"[vuex]未知突变类型:“
EN

Stack Overflow用户
提问于 2018-03-09 11:40:15
回答 5查看 16K关注 0票数 5

我是vue.js和vuex的新手,我对mapstate对象有一个问题,首先我在我的商店中只有一个模块:

代码语言:javascript
复制
-Store
 -index.js
 -mutations.js
 -actions.js
 -state.js

state.js :

代码语言:javascript
复制
export default {
    userInfo: {
        messages: [{ 1: 'test', 2: 'test' }],
        notifications: [],
        tasks: []
    }
}

因此,当我试图访问userInfo对象时,所有操作都是正确的:

代码语言:javascript
复制
computed: {
    ...mapState(["userInfo"]),
}

然后我决定创建模块:

代码语言:javascript
复制
-Store
 -modules
  -ldap.js
 -commons.js
 -index.js

所以userInfocommons.js文件中,现在当我尝试获取对象时,我总是得到undefined

commons.js

代码语言:javascript
复制
// state
const state = {
    userInfo: {
        messages: [{ 1: 'test', 2: 'test' }],
        notifications: [],
        tasks: []
    }
}

export default {
    actions,
    mutations,
    state  
}

Component.vue

代码语言:javascript
复制
computed: {
    ...mapState(["userInfo"]), // <---- undefined
}

main.js :

代码语言:javascript
复制
import Vue from 'vue'
import Vuex from 'vuex'
import commons from './commons'
import ldap from './modules/ldap'

Vue.use(Vuex)

export default new Vuex.Store({
    modules : {
        commons,
        ldap
    } 
})

你能告诉我如何访问userInfo对象吗?

谢谢。

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2018-03-09 12:31:28

考虑到:

  • 您的commons.js如下所示: // state ={ userInfo:{ messages:{ 1:'test',2:'test‘},通知:[],任务:[] }导出默认值{命名空间:真,// <==确保这是定义的操作、突变、状态}
  • main.js将其导入如下: 从' . / commons‘// .导出默认的新Vuex.Store({模块:{ commons })
  • 然后在Component.vue:上更新 从'vuex‘// .计算:{ ...mapState('commons',"userInfo"),// <==在这里添加模块名} 或 从'vuex‘const { mapState,mapActions }= createNamespacedHelpers('commons') // .注意上面的模块名称^计算:{ ...mapState("userInfo"),}

vuex未知突变类型:

由于您现在是命名空间您的commons模块,所以模块的突变现在必须是前缀

所以,假设你有变异,就像:

代码语言:javascript
复制
const mutations = {
    changeName(state, data) {
        state.name = data;
    }
}
export default {
    namespaced: true, 
    actions,
    mutations,
    state  
}

你用它就像:

代码语言:javascript
复制
this.$store.commit('changeName', "New Name");

现在就像这样使用它:

代码语言:javascript
复制
this.$store.commit('commons/changeName', "New Name");
票数 12
EN

Stack Overflow用户

发布于 2018-03-09 12:00:09

您必须将每个模块定义为一个单独的存储区,下面是一些伪示例。

代码语言:javascript
复制
// authStore.js

import mutations from './authMutations'
import actions from './authActions'
import getters from './authGetters'

const initialState = {
  ...
}

export default {
  state: initialState,
  mutations,
  actions,
  getters
}

然后,注册模块。

代码语言:javascript
复制
import authStore from './authStore'

const store = new Vuex.Store({
  modules: {
   {...authStore, namespaced: true},
   {...postStore, namespaced: true} // some other module defined like auth
  }
})

new Vue({
  ....
  store: store
})

然后,在组件上使用它:

代码语言:javascript
复制
import { createNamespacedHelpers } from 'vuex'

// map state and actions of the module
const { mapState, mapActions } = createNamespacedHelpers('auth')

export default {
  computed: {
     ...mapState({
       prop1: 'prop1'
     })
  }
}

Vuex模块文档

票数 1
EN

Stack Overflow用户

发布于 2018-03-09 12:07:51

我想您已经通过在模块中添加namespaced: true来命名模块。

因此,您应该将模块名作为第一个参数传递给mapState帮助程序,以便使用该模块作为上下文完成所有绑定。请参阅用名称空间绑定帮助程序

代码语言:javascript
复制
computed: { 
    ...mapState('commons' , ["userInfo"])
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49192961

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档