使用axios 報錯 TypeError: Cannot set property 'listData ' of undefined
報錯的主要原因是 在 then的內部不能使用vue實例化的this 因為在內部this 因為在內部 this
沒有被綁定
解決辦法:使用ES6箭頭函數,箭頭方法可以和父方法共享變量 ,或者在請求前 定義一下 let _self = this(里面使用到this的替換成_self就好了)
axios .get('https://api.coindesk.com/v1/bpi/currentprice.json') .then(response => { this.listData = response.data.list}) .catch(error => { console.log(error) this.errored = true }) .finally(() => this.loading = false) }