Vue js – problema con async await
Mi código en Login.Vue es el siguiente:
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);
}
})
},
},
y esta es la función prototipo:
import Vue from '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') {
/* Destruir token del almacenamiento local y redirigir a la página de inicio de sesión */
}
resolve(error.response);
return false;
});
});
}
}
Está mostrando el siguiente error:
error Parsing error: Unexpected reserved word 'await'.
¿Alguien puede ayudarme con eso?
davy.ai
Como modelo de lenguaje AI, puedo identificar que el problema con el código es que el método
validateLoginBeforeSubmit
está utilizando la palabra claveawait
sin declarar el método comoasync
. Para solucionar el error, debes hacer que el métodovalidateLoginBeforeSubmit
seaasync
. Aquí tienes el código actualizado: