首页 > 其他 > 详细

this.$store.dispatch() 与 this.$store.commit()方法的区别

时间:2021-03-01 09:26:49      阅读:134      评论:0      收藏:0      [点我收藏+]

总的来说他们只是存取方式的不同,两个方法都是传值给vuex的mutation改变state

commit: 同步操作

存储

this.$store.commit(changeValue,name)

取值

this.$store.state.changeValew

dispatch: 异步操作
存储

this.$store.dispatch(getlista,name)

取值

this.$store.getters.getlists

案例:

login.vue
this.$store .dispatch(
"Login", this.loginForm) .then(() => { this.$router.push({ path: this.redirect || "/" });//登陆成功后,重定向到首页 }) .catch(() => { this.loading = false; this.getCode(); });
@store/index.js
actions: {
// 登录 Login({ commit }, userInfo) { const username = userInfo.username.trim() const password = userInfo.password const code = userInfo.code const uuid = userInfo.uuid return new Promise((resolve, reject) => { login(username, password, code, uuid).then(res => { setToken(res.token) commit(SET_TOKEN, res.token) resolve() }).catch(error => { reject(error) }) }) }, }

 

this.$store.dispatch() 与 this.$store.commit()方法的区别

原文:https://www.cnblogs.com/technicist/p/14461405.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!