Vue.js通过Axios消费API给出错误:Uncaught(在promise中)TypeError:无法读取未定义的属性'protocol'

问题描述 投票:1回答:1

我试图通过VueJS中的Axios消费api。但是当我试图获取数据时它会出错。控制台日志(res.data)。需要你的帮助。看起来我错过了什么

未捕获(在承诺中)TypeError:无法读取未定义的属性'protocol'

这是API.Js中的代码

import axios from 'axios';
import API from '../API';



var urlLogin = API.url.host + '/login';

var login = {
    init: function(){
        this.vueConfig();
        if(localStorage.getItem('token') != null){
            window.location.replace("./input-mobile.html");
        }
    },
    vueConfig: function(){
        var app = new Vue({
            el: '#app',
            data: {
                isSubmit: false,
                email: "email",
                password: "password",
            },
            methods: {
                submitLogin: function(){
                    this.isSubmit = true;
                    axios.post()
                    axios({
                        method: 'post',
                        url: urlLogin,
                        data: {
                            email: this.email,
                            password: this.password
                        }
                    }).then(res =>{
                        console.log(res.data);
                        this.isSubmit = false;
                        localStorage.setItem("token", res.data.access_token);
                        localStorage.setItem("name", res.data.fullname);
                        window.location.replace("./input-mobile.html");
                    }, err =>{
                        console.log(err);
                        this.isSubmit = false;
                    });
                }
            }
        });
    }
}

module.exports = login;

API没问题。在浏览器api的网络(检查)中给出正确的响应。但它无法获取数据

javascript vue.js axios
1个回答
-1
投票

您在Axios回调中引用了错误的对象。尝试在let self = this方法的开头添加submit然后在回调中,通过this.isSubmit更改self.isSubmit

submitLogin: function(){
  let self = this;
  this.isSubmit = true;
  axios.post()
  axios({
     method: 'post',
     url: urlLogin,
     data: {
         email: this.email,
         password: this.password
     }
  }).then(res =>{
     console.log(res.data);
     self.isSubmit = false;
     localStorage.setItem("token", res.data.access_token);
     localStorage.setItem("name", res.data.fullname);
     window.location.replace("./input-mobile.html");
  }, err =>{
     console.log(err);
     self.isSubmit = false;
  });
}
© www.soinside.com 2019 - 2024. All rights reserved.