在使用axios经常会出现这个问题
如下面代码
data(){
return{
goods:{
title:null,
subTitle:null,
originalCost:null,
currentPrice:null,
discount:null,
isFreeDelivery:null
}
}
},
mounted:function()
{//this is undefined
axios.get("http://localhost:8081/get/goods/749").then(function (response){
console.log(response);
var _this = this;
_this.goods = response.data;
});
修改后:只需要将function换成=> 箭头函数就可以了
mounted:function()
{
axios.get("http://localhost:8081/get/goods/749").then((response)=>{
console.log(response);
var _this = this;
_this.goods = response.data;
});