为了账号安全,请及时绑定邮箱和手机立即绑定

这些代码怎样复用?

这些代码怎样复用?

qq_杀意隆_0 2017-10-11 14:55:06
这是微信小程序的代码1.if(!this.data.serveItem){                                    that.setData({                          loadingHide:false                     })                      //请求服务项目列表资源                     wx.request({                         url:'https://bird.ioliu.cn/v1?url=http://aasss1.oschina.io/122/4.json',                         success(res){                             that.setData({                                  serveItem:res.data,                                  tireServices: res.data.tireServices,                                  maintainServices: res.data.maintainServices,                                  installServices: res.data.installServices,                             })                             getElementHeight('#item-wrap1',that)                         }                     })                 }2.if(!this.data.rates){                     that.setData({                         loadingHide:false                     })                     // 请求评价列表资源                     wx.request({                         url:'https://bird.ioliu.cn/v1?url=http://aasss1.oschina.io/122/2.json',                         success(res){                             that.setData({                                 rates:res.data.rates                             })                             getElementHeight('#item-wrap2',that)                         }                     })                 }3.if(!this.data.recommendList){                     that.setData({                         loadingHide:false                     })                     // 请求推荐列表资源                     wx.request({                         url:'https://bird.ioliu.cn/v1?url=http://aasss1.oschina.io/122/recommendList.json',                         success(res){                             that.setData({                                 recommendList:res.data.recommendList                             })                             getElementHeight('#item-wrap3',that)                         }                     })                 }上面3段代码重复性高,于是我写了个函数:function requestResouce(that,oldObj,newObj,url,id){     if(!oldObj){         that.setData({             loadingHide:false         })         wx.request({             url:url,             success(res){                 that.setData(newObj)                 getElementHeight(id,that)             }         })     } }结果代入参数后提示res is not defined。到底要怎样写才不会出错?
查看完整描述

1 回答

?
Gotta

TA贡献53条经验 获得超16个赞

控制台你看你第几行报错了。似乎不是你写的函数报错,估计方法调来调去,传参哪里传错了。

查看完整回答
反对 回复 2017-10-11
  • qq_杀意隆_0
    qq_杀意隆_0
    提示res is not defined
  • Gotta
    Gotta
    是啊 提示有一大坨。应该有告诉你是哪个方法报错吧? 比如这样 abc is not defined;at pages/index/index page test function ReferenceError: abc is not defined 看那个at
  • 1 回答
  • 0 关注
  • 1017 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信