1 回答
TA贡献1877条经验 获得超6个赞
创建的钩子内的代码可以位于名为的单独方法中getUsers,然后在方法中调用它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);
}
},
},
添加回答
举报