1 回答

TA貢獻(xiàn)1877條經(jīng)驗(yàn) 獲得超6個(gè)贊
創(chuàng)建的鉤子內(nèi)的代碼可以位于名為的單獨(dú)方法中g(shù)etUsers,然后在方法中調(diào)用它EnvioLogin:
import axios from "axios";
export default {
name: "login",
data() {
return {
showError: false,
email: "",
password: "",
};
},
created() {
this.getUsers();
},
methods: {
async getUsers(){
const response = await axios.get("api/users", {
headers: {
Authorization: "Bearer " + localStorage.getItem("token")
}
});
console.log(response);
},
async EnvioLogin() {
try {
const response = await axios.post("api/auth/login", {
email: this.email,
password: this.password,
});
localStorage.setItem("token", response.data.token);
const status = JSON.parse(response.status);
if (status == "200") {
this.getUsers();
console.log(response);
this.$router.push("intermediorotas");
this.showLogin = false;
}
} catch (error) {
this.showError = true;
setTimeout(() => {
this.showError = false;
}, 2000);
}
},
},
添加回答
舉報(bào)