vuex 使用this.$store.commit()提示不是function

1、使用vuex 在组件里使用this.$store.commit()提示不是function

在enter.vue中

        created () {
            this.$store.commit('changeIndexConf', {
                isTab: true,
                isBack: false,
                title: ''
            });
        }

3.store.js

import Vue from 'vue';
import Vuex from 'vuex';
Vue.use(Vuex)
const store = new Vuex.Store({
    state: {
        //公共
        comm: {
            loading: false,
            login: {
                memberId: '',
                userData: ''
            },
            indexConf: {
                isTab: false, //是否显示tab页
                isBack: false, //是否显示返回
                title: '' //标题
            }
        }
    },
    mutations: {
        /*
         * 修改header的信息
         *比如是否有回退按钮,标题显示内容
         * */
        changeIndexConf: (state, data) => {

            Object.assign(state.comm.indexConf, data);
        }
    },
    actions: {     
    },
    getter: {
    }
});
export default store

4、提示

enterpiseList.vue?c518:15 Uncaught TypeError: this.$store.commit is not a function

阅读 25.6k
3 个回答

我刚也遇到了这个问题,打开你的package.json,看看vuex是不是装成了1.0版本

可能你没有注册成全局的。

//render page
const root = new Vue({
    router,
    store, //注册成全局的
    ...Page
}).$mount('#root');

在组件中使用建议:

 methods: {
     ...mapMutations('filter', ['updateIndustry','resetData'])
}

这样就可以和组件内部函数一样调用了。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题