收藏
回答

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)
回答关注问题邀请回答
收藏

2 个回答

  • 游戏人生
    游戏人生
    2022-01-28

    this 对象作用域不对。。。

    要么外部 let _this = this;

    要么 箭头函数 res=>{}

    2022-01-28
    有用 2
    回复 1
    • l'm
      l'm
      2022-01-28
      3Q~
      2022-01-28
      回复
  • Mr.Zhao
    Mr.Zhao
    2022-01-28
    success: res => {} 用箭头函数,两年了吧,这个问题才遇到?
    
    2022-01-28
    有用 1
    回复 2
    • 游戏人生
      游戏人生
      2022-01-28
      哈哈,你非要加个时间
      2022-01-28
      回复
    • l'm
      l'm
      2022-01-28
      业余开发。。。
      2022-01-28
      回复
登录 后发表内容