大佬帮忙看下为什么报错this.setData is not a function;?

//wxml
<view class="container">
<block wx:if="{{position}}">

      <web-view src="https://wechat.sghcloud.com"></web-view>

</block>

</view>
//js
const app = getApp()

Page({

data: {

position: false

},

onLoad: function () {

wx.getLocation({
  type: 'wgs84',
  success: function (res) {
    var latitude = res.latitude
    var longitude = res.longitude
    var speed = res.speed
    var accuracy = res.accuracy
    console.log(latitude, longitude)
    if (latitude > 22 && latitude < 23 && longitude > 112.028 ) {
      this.setData({
        position: true
      })
    } else {
      this.setData({
        position: true
      })
        wx.showLoading({
          title: '等待中',
        })

    }
  }
})

},

})

阅读 2.9k
1 个回答

首先: 没有写过小程序

原因是回调里面的this指向变了

解决

  1. 用箭头函数
  success: (res)=> {}
  1. 保存this指向
 onLoad: function() {
    var that = this
    wx.getLocation({
      type: 'wgs84',
      success: function(res) {
        that.setData({
          position: true,
        })
      },
    })
  },
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题