gpt4 book ai didi

vue.js - 仅在 VueJS 中完成分派(dispatch)和提交后才触发路由

转载 作者:行者123 更新时间:2023-12-03 06:46:51 27 4
gpt4 key购买 nike

我确实有一个表单提交,它接受电子邮件和密码,然后将它们传递到商店中名为 userSignIn

的操作中

登录.vue:

onSubmit () {
if (this.$refs.form.validate()) {
const user = {
email: this.email,
password: this.password
}
this.$store.dispatch('userSignIn', user)
.then(() => {
this.$router.push('/')
}).catch(err => {
console.log(err)
})
}
}

在商店中,我确实有一个像这样的 userSignIn 操作

store.js 操作:

userSignIn ({commit, getters}, payload) {
getters.Api.post(`user/signin`, {
email: payload.email,
password: payload.password
}).then(res => {
commit('userSignIn', res.data.token)
}).catch(err => {
console.log(err)
})
}

路由(this.$router.push('/')) 只能在userSignIn commit(commit('userSignIn', res .data.token))。但是实际发生的路由在提交之前触发,这会导致错误,因为用户 token 尚未设置。

如何仅在完成 ​​dispatchcommit 后触发某些东西(在本例中为 this.$router.push('/')) 在里面?

最佳答案

返回 promise 就成功了。

userSignIn ({commit, getters}, payload) {
return getters.Api.post(`user/signin`, {
......
})

关于vue.js - 仅在 VueJS 中完成分派(dispatch)和提交后才触发路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49113023/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com