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

不羁岁月 提交于 2020-02-17 07:46:01

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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!