在vue-cli中使用axios拿取數據時
export default {
data(){
return{
lists:{
}
}
},
methods: {
getList(){
axios.get('https://cnodejs.org/api/v1/topics',{
})
.then(function(response){
window.console.log(response.data.data);
this.lists = response.data.data;
})
.catch(function (error) {
window.console.log(error);
})
}
},
beforeMount() {
this.getList();
},
報錯
TypeError: Cannot set property 'lists' of undefined at eval (list.vue?51fc:19)
能get到數據,但無法傳給lists數組
報錯信息lists未定義,查找調試許久,發現是this指向問題。
在vue-cli里.then里使用function就會亂了this指向
把.then里function改用es6的寫法就能正常傳參了
.then((response)=>{ window.console.log(response.data.data); this.lists = response.data.data; })
為什么用function定義就會亂this指向,奈何一時半會兒也沒搞懂,留此疑惑,日后解答