我在 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'.
任何人都可以幫助我嗎?
uj5u.com熱心網友回復:
你不應該混合和匹配async/await傳統的thenpromise 處理,因為它會導致混淆。
插入嘗試:
async validateLoginBeforeSubmit () {
var result = await this.$validator.validate()
if (result) {
var data = {
email: this.email,
password: this.password
}
var response = await this.$api('POST', 'login', data);
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/395511.html
標籤:javascript Vue.js 异步等待 承诺
上一篇:為什么這個計算屬性的更新在我的Vue3應用程式中失敗?
下一篇:定義要在組件內部使用的全域變數
