【发布时间】:2019-04-11 04:48:37
【问题描述】:
我正在处理网站的认证部分,这是用户发送连接表单时调用的方法:
login() {
this.$store.dispatch('RETRIEVE_TOKEN', {
username: this.username,
password: this.password,
})
}
还有行动:
RETRIEVE_TOKEN(context, credentials) {
return new Promise((resolve, reject) => {
axios.post('someurl/auth', {
email: credentials.username,
password: credentials.password
})
.then(response => {
const token = response.data.key
localStorage.setItem('key', token)
context.commit('retrieveToken', token)
resolve(response)
this.$router.push('/backoffice')
})
.catch(error => {
console.log(error);
reject(error)
});
})
}
我的问题是即使用户发送了错误的密码和用户邮件,也会调用this.$router.push('/backoffice')。我不明白为什么。谁能给我解释一下?
【问题讨论】:
-
这种情况下响应对象的内容是什么?
-
你为什么要返回一个 Promise?
-
承诺没有被拒绝。您需要检查 400 错误的响应并进行相应处理。
-
只需
console.log回复并检查您得到了什么。此外,无需返回 Promise:RETRIEVE_TOKEN(context, credentials) { return axios.post('/someurl/auth', ... -
离题但 axios 返回一个承诺,因此您应该避免创建另一个承诺,因为这是一个 anti-pattern。
标签: javascript vue.js promise axios vuex