方法里用this和方法里的方法里用this,指向的內容不一樣
不使用箭頭函數
created(){
let _this=this;
axios.get('http://localhost:8081/findAll')
.then(function(response) {
_this.users = response.data;
console.log(_this.users);
}).catch(function (error) {
console.log(error);
})
}
可以用箭頭函數代替_this。
箭頭函數錯誤用法
created:()=>{
axios.get('http://localhost:8081/findAll')
.then( function(response) {
this.users = response.data;
}).catch(function (error) {
console.log(error);
})
}
箭頭函數正確用法
created(){
axios.get('http://localhost:8081/findAll')
.then( (response)=> {
this.users = response.data;
}).catch(function (error) {
console.log(error);
})
}