天天看點

關于mapState和mapMutations和mapGetters 和mapActions輔助函數的用法及作用(二)-----mapMutations

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必須是同步函數!!!

繼續閱讀