您现在的位置:首页 >> 前端 >> 内容

Mutations修改状态介绍

时间:2018/1/2 11:02:53 点击:

  核心提示:第一节我们初步见识了Mutations,这节我们再来详细看看,首先我们在store里面的mutations的方法加个参数const mutations = {add(state,n){state.co...

第一节我们初步见识了Mutations,这节我们再来详细看看,首先我们在store里面的mutations的方法加个参数

const mutations = {
    add(state,n){
        state.count+=n;
    },
    reduce(state){
        state.count--;
    }
}

然后是我们的Count.vue组件



<script>
    import store from '@/vuex/store'
    //这里多了个mapMutations
    import { mapState,mapMutations } from 'vuex'
    export default {
        data(){
            return {
                msg: 'Hello Vuex!'
            }
        },
        computed:mapState(['count']),
        //将mutations添加到menthods里面
        methods:mapMutations(['add','reduce']),
        //将store添加到实例
        store
    }
</script>

提示:这里我们可以看看我们调用方法时和声明方法的参数是有区别的

add(state,n);//声明

add(10);//调用,state是不用传的

//按我们第一节调用的,也是不一样的
$store.commit('add',10);

Tags:MU UT TA AT 
作者:网络 来源:z858466的博客