【发布时间】:2022-01-25 03:30:15
【问题描述】:
我在 Login.Vue 中的代码如下
methods: {
async validateLoginBeforeSubmit () {
this.$validator.validate().then((result) => {
if (result) {
var data = {
email: this.email,
password: this.password
}
var response = await this.$api('POST', 'login', data);
}
})
},
},
这是原型函数
从'vue'导入Vue;
Vue.prototype.$api = async() => function(method, url, data){
if (method == 'POST') {
return new Promise((resolve) => {
this.$http.post(url, data).then((response) => {
resolve(response);
}).catch((error) => {
if (error.response.status == '401') {
/* Destroy token from local storage and redirect to login page */
}
resolve(error.response);
return false;
});
});
}
}
下面报错
error Parsing error: Unexpected reserved word 'await'.
谁能帮我做同样的事?
【问题讨论】:
-
你没有在异步函数中使用 await。
-
避免
Promiseconstructor antipattern 中的$api!
标签: javascript vue.js async-await promise