往往在登录请求中会进行了异步操作所以导致返回了一个promise对象。
Login() {
this.$refs.loginFormRef.validate(valid => {
if(!valid) return;
const { data : res } = this.$http.post('login',this.loginForm);
if(res.meta.status !== 200){
this.$message({ // message需要在配置文件中以原型的方式添加
message: '登录失败',
type: 'error'
})
}
}
这个时候就要用到await async来简化promise,需要注意的是await只能用在被async修饰的方法中,所以在函数前面必须加上async才能使用。await的作用在这里是等待post请求结束再进行下一步,避免进行异步操作。
Login() {
this.$refs.loginFormRef.validate(async valid => {
if(!valid) return;
const { data : res } = await this.$http.post('login',this.loginForm);
if(res.meta.status !== 200){
this.$message({ // message需要在配置文件中以原型的方式添加
message: '登录失败',
type: 'error'
})
}
}