在组件中提交Mutations:

import { mapState, mapMutations } from 'vuex'
export default {
    data() {
        return {
            msg: "vuex要点"
        }
    },
    store,
    computed: mapState([
        'count'
    ]),
    // methods: mapMutations([
    //     'add', 'reduce'
    // ]),

    //或者
    methods: {
        //如果组件中事件的名称和mutations中方法的名称相同,可以传一个字符串数组
        ...mapMutations([
            'add' //映射 this.add() 为 this.$store.commit('add')
        ]),
        //组件中的事件名和mutations中的方法名不一样,传入对象
        ...mapMutations({
            reduces: 'reduce' //映射 $this.reduces 为 this.store.commit('reduce')
        })
    }
}

Mutations必须是同步函数!!!

相关文章: