欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

微信小程序 調(diào)用遠(yuǎn)程接口 給全局?jǐn)?shù)組賦值代碼實(shí)例

 更新時(shí)間:2019年08月13日 17:09:38   作者:賈樹(shù)丙  
這篇文章主要介紹了微信小程序 調(diào)用遠(yuǎn)程接口 給全局?jǐn)?shù)組賦值代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

關(guān)鍵是 let that = this

因?yàn)樵趏nLoad 的 wx.request方法里, this指的是 wx.request 的上下文, 所以應(yīng)該是在 onLoad 的時(shí)候定義 let that = this

調(diào)用遠(yuǎn)程服務(wù),序列化后,把obj賦值給bookList

最開(kāi)始的報(bào)錯(cuò)代碼如下:

Page({
 data: {
  bookList: []
 },
 onLoad: function() {
  wx.request({
   url: 'https://jiashubing.cn/wechat/book',
   header: {
    'content-type': 'application/json'
   },
   success(res) {
    var obj = JSON.parse(res.data)
    console.log(obj)
    this.setData({
     bookList: obj
    })
   }
  })
 }
})

報(bào)錯(cuò)為:

VM3293:1 thirdScriptError
Cannot read property 'setData' of null;at pages/index/index onLoad function;at api request success callback function
TypeError: Cannot read property 'setData' of null

報(bào)錯(cuò)原因是this 指向有問(wèn)題,得在onload的時(shí)保存實(shí)例指向,let that = this,下面全用that,this指向永遠(yuǎn)值得是自己的上下文環(huán)境

正確代碼如下:

Page({
 data: {
  bookList: []
 },
 onLoad: function() {
  let that = this
  wx.request({
   url: 'https://jiashubing.cn/wechat/book',
   header: {
    'content-type': 'application/json'
   },
   success(res) {
    var obj = JSON.parse(res.data)
    console.log(obj)
    that.setData({
     bookList: obj
    })
   }
  })
 }
})

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論