获取this.$store.dispatch的返回值
【摘要】 this.$store.dispatch() 是用来传值给vuex的mutation改变state。
我们来看看怎么获取this.$store.dispatch() 调用的返回值。
Action
首先看看定义的Action:
login({ commit }, userInfo) { // const { username, password } = u...
this.$store.dispatch() 是用来传值给vuex的mutation改变state。
我们来看看怎么获取this.$store.dispatch() 调用的返回值。
Action
首先看看定义的Action:
login({ commit }, userInfo) { // const { username, password } = userInfo return new Promise((resolve, reject) => { login(userInfo).then(response => { const { data } = response commit('SET_TOKEN', data.token) setToken(data.token) resolve(response) }).catch(error => { reject(error) }) })
},
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
两个关键点:
- 返回一个new Promise
return new Promise((resolve, reject)
- 1
- resolve函数中传入返回值
resolve(response)
- 1
调用
this.$store.dispatch('user/login', this.loginForm) .then(res => { console.log(res) fullLoading.close(); //登陆首页还是之前访问需要重定向的地址 this.$router.push({ path: this.redirect || '/' }) this.loading = false }) .catch(error => {}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
在调用里就可以直接通过 res 来直接获取返回值了。
.then(res => { console.log(res)
- 1
- 2
参考:
【1】:如何获取vuex的action的返回值(axios请求为例)
【2】:vuex中 this.$store.dispatch() 与 this.$store.commit()方法的区别
文章来源: blog.csdn.net,作者:三分恶,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/sinat_40770656/article/details/109996351
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)