vuex中commit与dispatch的使用场景

this.$store.commit('stestvalue', 1);

this.$store.dispatch('stestvalue', true);

规范的使用方式:

// 以载荷形式
store.commit('increment',{
amount: 10 //这是额外的参数
})

// 或者使用对象风格的提交方式
store.commit({
type: 'increment',
amount: 10 //这是额外的参数
})

主要区别:
dispatch:含有异步操作,数据提交至 actions ,可用于向后台提交数据

对应的函数在actions;

this.$store.dispatch('SETTESTVALUE', true);

commit:同步操作,数据提交至 mutations ,可用于读取用户信息写到缓存里

对应的函数在mutations;

this.$store.commit('INC', 1);

具体情况,可以根据业务使用,进行调用commit和dispatch;


版权声明:本文为violetlength原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。