html52.com
我爱小程序-开发者交流社区

wx.getLocation获取变量后怎么设置到全局?


   wx.getLocation({
      type'wgs84',
      success (res) {
        this.setData({
        latitude : res.latitude,
       longitude : res.longitude,
       accuracy : res.accuracy,
      })
      }  
       })


VM3449 WAService.js:2 thirdScriptError
Cannot read property 'setData' of undefined;at pages/add/add onLoad function;at api getLocation success callback function
TypeError: Cannot read property 'setData' of undefined
    at success (http://127.0.0.1:40267/appservice/pages/add/add.js:412:14)
    at Function.o.<computed> (http://127.0.0.1:40267/appservice/__dev__/WAService.js?t=wechat&s=1643331694522&v=2.10.2:2:1388831)
    at Object.success (http://127.0.0.1:40267/appservice/__dev__/WAService.js?t=wechat&s=1643331694522&v=2.10.2:2:126419)
    at y (http://127.0.0.1:40267/appservice/__dev__/WAService.js?t=wechat&s=1643331694522&v=2.10.2:2:528980)
    at v (http://127.0.0.1:40267/appservice/__dev__/WAService.js?t=wechat&s=1643331694522&v=2.10.2:2:529218)
    at http://127.0.0.1:40267/appservice/__dev__/WAService.js?t=wechat&s=1643331694522&v=2.10.2:2:530772
    at http://127.0.0.1:40267/appservice/__dev__/asdebug.js:1:48487
    at C (http://127.0.0.1:40267/appservice/__dev__/asdebug.js:1:48142)
    at http://127.0.0.1:40267/appservice/__dev__/asdebug.js:1:3205
    at Set.forEach (<anonymous>)(env: Windows,mp,1.05.2111300; lib: 2.10.2)

网友回复

圣殿骑士:

this 对象作用域不对。。。

要么外部 let _this = this;

要么 箭头函数 res=>{}

Mr.Zhao:
success: res => {} 用箭头函数,两年了吧,这个问题才遇到?

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

真正的个人免签约支付接口

云免签H5支付