Vue報TypeError:this.$setisnotafunction錯誤怎么辦

這篇文章主要為大家展示了“Vue報TypeError: this.$set is not a function錯誤怎么辦”,內容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領大家一起研究并學習一下“Vue報TypeError: this.$set is not a function錯誤怎么辦”這篇文章吧。

創(chuàng)新互聯(lián)-專業(yè)網站定制、快速模板網站建設、高性價比孫吳網站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式孫吳網站制作公司更省心,省錢,快速模板網站建設找我們,業(yè)務覆蓋孫吳地區(qū)。費用合理售后完善,十余年實體公司更值得信賴。

報錯場景:將APi中得到的response數(shù)據(jù),用Vue$set()使數(shù)據(jù)動態(tài)響應

報錯代碼:

 methods: {
  textTranslate: function (text, to) {
 
   $.ajax({
    url: 'http://openapi.youdao.com/api',
    type: 'post',
    dataType: 'jsonp',
    data: {
     q: text,
     appKey: this.appKey,
     salt: this.salt,
     from: this.from,
     to: to,
     sign: md5(this.appKey + text + this.salt + this.key)
    },
    success: function (data) {
     this.$set(this.$data, 'translatedText', data.translation[0])
    }
   })
  }
 }

報錯原因:這里的this指向的不是VueModel,

解決方法1:在執(zhí)行函數(shù)中定義指向Model的變量 let vm = this ,用該變量替代this

 methods: {
  textTranslate: function (text, to) {
   let vm = this
   $.ajax({
    url: 'http://openapi.youdao.com/api',
    type: 'post',
    dataType: 'jsonp',
    data: {
     q: text,
     appKey: this.appKey,
     salt: this.salt,
     from: this.from,
     to: to,
     sign: md5(this.appKey + text + this.salt + this.key)
    },
    success: function (data) {
     vm.$set(vm.$data, 'translatedText', data.translation[0])
    }
   })
  }
 }

解決方法2:將。siccess改為箭頭函數(shù)的寫法,這樣子箭頭函數(shù)里的this其實是指向VueModel的,這樣子用this看不會報錯了

success: (data) => {
     this.$set(this.$data, 'translatedText', data.translation[0])
    }

以上是“Vue報TypeError: this.$set is not a function錯誤怎么辦”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注創(chuàng)新互聯(lián)行業(yè)資訊頻道!

當前題目:Vue報TypeError:this.$setisnotafunction錯誤怎么辦
地址分享:http://bm7419.com/article30/pcosso.html

成都網站建設公司_創(chuàng)新互聯(lián),為您提供網站設計小程序開發(fā)、網頁設計公司、外貿建站、關鍵詞優(yōu)化、外貿網站建設

廣告

聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

手機網站建設