目录获取this.$store.dispatch的返回值Action调用根据Vuex的this.$store.dispatch()返回值处理逻辑App.vuevuex/store/a
this.$store.dispatch() 是用来传值给vuex的mutation改变state。
我们来看看怎么获取this.$store.dispatch() 调用的返回值。
首先看看定义的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)
})
})
},
两个关键点:
return new Promise((resolve, reject)
resolve(response)
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 => {}
在调用里就可以直接通过 res 来直接获取返回值了。
.then(res => {
console.log(res)
const ret = await this.$store.dispatch('userLogin', {
username: this.curUserName,
password: this.curPassword
})
if (ret && ret.info) {
this.$message.success(ret.info)
await this.$store.dispatch('controlLoginDialog', false)
} else {
this.$message.warning(ret)
}
async userLogin ({commit}, account) {
let userInfo = {}
return new Promise((resolve, reject) => {
requestUserLogin(account).then(response => {
if (response.status === 200) {
if (response.data.data) {
userInfo = response.data.data
userInfo.userName = userInfo.name
userInfo.isLogin = true
resolve({
info: userInfo.userName + ' 登录成功,欢迎进入百度云智学院实验平台'
})
} else if (response.data.fail) {
userInfo.userName = ''
userInfo.isLogin = false
myConsole('response.data.fail')
resolve(response.data.fail)
}
} else {
userInfo.userName = ''
userInfo.isLogin = false
}
commit(USER_LOGIN, {userInfo})
}).catch(err => {
myConsole(err)
reject(err)
})
})
},
以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。
--结束END--
本文标题: 如何获取this.$store.dispatch的返回值
本文链接: https://www.lsjlt.com/news/177281.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-01-12
2023-05-20
2023-05-20
2023-05-20
2023-05-20
2023-05-20
2023-05-20
2023-05-20
2023-05-20
2023-05-20
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0