Vue中axios.then抓不到變數undefined問題

  • 1060
  • 0
  • 2019-07-20

Vue

在Vue裡面使用 axios時會發現一般寫法

axios.post("", {
          query: `{
              getdata {
                id
              }
            }`
        })
        .then(function(res) {
         this.data = res;
})

這時會抓不到 this.data 的情況發生

這時需要將語法改成

axios.post("", {
          query: `{
              getdata {
                id
              }
            }`
        })
        .then(res=> {
         this.data = res;
})

如此就可以成功將值寫入 this.data 中了