vue.js - Vuex mapstate 对象未定义和 "[vuex] unknown mutation type: "

标签 vue.js vuejs2 vuex vuex-modules

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

-Store
 -index.js
 -mutations.js
 -actions.js
 -state.js

状态.js :

export default {
    userInfo: {
        messages: [{ 1: 'test', 2: 'test' }],
        notifications: [],
        tasks: []
    }
}

因此,当我尝试访问 userInfo 对象时,一切正常:

computed: {
    ...mapState(["userInfo"]),
}

然后我决定创建模块:

-Store
 -modules
  -ldap.js
 -commons.js
 -index.js

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

commons.js

// state
const state = {
    userInfo: {
        messages: [{ 1: 'test', 2: 'test' }],
        notifications: [],
        tasks: []
    }
}

export default {
    actions,
    mutations,
    state  
}

Component.vue

computed: {
    ...mapState(["userInfo"]), // <---- undefined
}

ma​​in.js :

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 对象吗?

谢谢。

最佳答案

考虑:

  • 你的commons.js如下:

    // state
    const state = {
        userInfo: {
            messages: [{ 1: 'test', 2: 'test' }],
            notifications: [],
            tasks: []
        }
    }
    
    export default {
        namespaced: true, // <== make sure this is defined
        actions,
        mutations,
        state  
    }
    
  • ma​​in.js 像这样导入它:

    import commons from './commons'
    // ..
    export default new Vuex.Store({
        modules : {
            commons,
            ldap
        } 
    })
    
  • 然后更新Component.vue:

    import { mapState } from 'vuex'
    
    // ...
    
    computed: {
        ...mapState('commons', ["userInfo"]), // <== add module name here
    }
    

    或者

    import { createNamespacedHelpers } from 'vuex'
    
    const { mapState, mapActions } = createNamespacedHelpers('commons')
    
    // ...                          notice module name above ^^^^^^^^^
    
    computed: {
        ...mapState(["userInfo"]),
    }
    

"[vuex] unknown mutation type: "

由于您现在正在命名您的 commons 模块,因此该模块的突变现在必须加前缀

所以,假设你有一个像这样的突变:

const mutations = {
    changeName(state, data) {
        state.name = data;
    }
}
export default {
    namespaced: true, 
    actions,
    mutations,
    state  
}

然后你像这样使用它:

this.$store.commit('changeName', "New Name");

现在像这样使用它:

this.$store.commit('commons/changeName', "New Name");

关于vue.js - Vuex mapstate 对象未定义和 "[vuex] unknown mutation type: ",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49192961/

相关文章:

vue.js - Vuetify 数据表错误会在我强制重新渲染时解决

javascript - 为什么在这种情况下我不能过滤本地 json 文件 (vuejs2)

javascript - 如何在VueJS中按对象属性过滤对象数组

css - Vue.js CSS 包优先级(?)

javascript - 通过标签过滤数组 Vuejs

javascript - 在 Vue 动态添加的组件中添加和删除元素的问题

vue.js - 如何在vue中通过条件模板分离开始和结束标签而不出现编译错误?

javascript - 在 javascript django 中使用 python 变量的最佳实践

reactjs - 在 Vue/Vuex 中是否有类似 mapStateToProps 的 react/redux 方式

arrays - 使用数组排序重新排列 vue 中的元素在移动设备上不起作用?