我试图在VueJS中使用api thorugh Axios。但是当我尝试获取数据时,它给出了错误。控制台日志(res.data)。需要你的帮助。好像我错过了什么
未捕获(承诺)TypeError:无法读取未定义的属性“协议”
这是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的网络(检查)中给出正确的响应。但是它无法获取数据
答案 0 :(得分:-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;
});
}