鍍金池/ 問答/HTML/ 請幫忙解決如下問題

請幫忙解決如下問題

完全沒有辦法,請教各位大神如何解決

data(){
            return {
                goodsList:[]
            }
        },
        created(){
        axios.get("/goods")
          .then(function (response) {
            var res=response.data
             if(res.status=="0"){
               this.goodsList=res.result.list
             }else{
              this.goodsList=[]
             }
          })
      },
        
                

后臺數(shù)據(jù)是能獲取到的但是報錯信息 Uncaught (in promise) TypeError: Cannot set property 'goodsList' of undefined
就是this.goodsList=res.result.list這里出的問題改了名字也一樣沒有辦法解決
請求大神

回答
編輯回答
枕頭人

this, this, this

2018年8月12日 20:03
編輯回答
澐染

原因是axios無法獲取到this
解決辦法

 created(){
         var a=this
        axios.get("/goods")
          .then(function (response) {
            var res=response.data
             if(res.status=="0"){
               a.goodsList=res.result.list
             }else{
              a.goodsList=[]
             }
          })

就可以了

2017年1月6日 09:10
編輯回答
陌上花

在js里用this的時候一定要小心,因為這個this通常指的是調(diào)用這個函數(shù)的對象,當你無法確定是誰調(diào)用了這個函數(shù),就要在這個函數(shù)的外面,將你要用到的this所指的對象賦值給一個變量,然后將這個變量在函數(shù)內(nèi)部使用
在你的這段代碼中,正如樓上的回答,先在axios.get()執(zhí)行前,將this也就是這個vue對象賦值給avar a = this
關于this相關的我寫過一篇文章,有空可以看看哈
javascript對象不完全探索記錄01:this! which?- lskrat

2018年3月24日 09:17
編輯回答
萌吟
    created(){
        var _this = this;
        axios.get("/goods")
          .then(function (response) {
            var res=response.data
             if(res.status=="0"){
               _this.goodsList=res.result.list
             }else{
              _this.goodsList=[]
             }
          })
2017年9月17日 17:58