亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Axios:用戶在登錄前嘗試

Axios:用戶在登錄前嘗試

30秒到達戰場 2023-09-14 18:17:03
當我使用正確的電子郵件/密碼發送“EnvioLogin”時,我從“/login”獲取訪問令牌,但之后我無法獲取“/users”,只有在我在瀏覽器中檢查時,我才得到:https://i . stack.imgur.com/yBJK0.jpg https://i.stack.imgur.com/bWQEG.jpg用戶嘗試登錄后如何運行創建的掛鉤?import axios from "axios";export default {  name: "login",  data() {    return {      showError: false,      email: "",      password: "",    };  },     async created() {    const response = await axios.get("api/users", {      headers: {        Authorization: "Bearer " + localStorage.getItem("token")      }    });    console.log(response);  },  methods: {    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") {          console.log(response);          this.$router.push("intermediorotas");          this.showLogin = false;        }      } catch (error) {        this.showError = true;        setTimeout(() => {          this.showError = false;        }, 2000);      }    },  },
查看完整描述

1 回答

?
慕哥9229398

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);

      }

    },

  },


查看完整回答
反對 回復 2023-09-14
  • 1 回答
  • 0 關注
  • 95 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號