天天看点

快应用fetch 回调中无法调用外部函数

当fetch访问服务器返回数据触发success方法时,发现在success方法中无法使用this下的外部元素与涵数解决方法:const that = this

const that = this  //传递this到内部使用that代替
      fetch.fetch({
      url: urls,
      data: {
        id: 'LocalNews',
        ajax: 'json'
      },
      responseType: 'json',// 支持返回类型是text、json、file和arraybuffer,默认返回字符串文本。
      success: function (ret) {
        memList = memList.concat(ret.data.data)
        const list = memList.splice(0, that.size)
        
        that.productList = that.productList.concat(list)//在内部使用that来访问外部内容
      
      }
}