关于mapState和mapMutations和mapGetters 和mapActions辅助函数的用法及作用(二)-----mapMutations

在组件中提交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必须是同步函数!!!

原文地址:https://www.cnblogs.com/lhl66/p/8261719.html