鍍金池/ 問答/HTML/ 大佬幫忙看下為什么報錯this.setData is not a functio

大佬幫忙看下為什么報錯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: '等待中',
        })

    }
  }
})

},

})

回答
編輯回答
避風(fēng)港

首先: 沒有寫過小程序

原因是回調(diào)里面的this指向變了

解決

  1. 用箭頭函數(shù)
  success: (res)=> {}
  1. 保存this指向
 onLoad: function() {
    var that = this
    wx.getLocation({
      type: 'wgs84',
      success: function(res) {
        that.setData({
          position: true,
        })
      },
    })
  },
2018年2月26日 13:52