在调用方法过程中 报错如下:
代码如下:
Page({ /** * 页面的初始数据 */ data: { userLocalInfo:'用户地址' }, /** * 返回swapping页面 */ backSwapping: function(){ wx.navigateBack({ delta:1 }) }, /** * 获取地理位置 */ getLocalInfo: function(){ wx.getLocation({ type: "wgs84" , altitude:true, success: function(obj){ this.setData({ userLocalInfo:"经度:"+obj.latitude+"\n纬度:" }) } }) } })
报错原因:
因为在success回调方法中,success函数实际是一个闭包 , 无法直接通过this来setData
所以 要解决这个问题
解决方法:
扫描二维码关注公众号,回复:
994924 查看本文章
提前在success回调函数外就获取到this,然后在回调函数中使用
Page({ /** * 页面的初始数据 */ data: { userLocalInfo:'用户地址' }, /** * 返回swapping页面 */ backSwapping: function(){ wx.navigateBack({ delta:1 }) }, /** * 获取地理位置 */ getLocalInfo: function(){ var that = this wx.getLocation({ type: "wgs84" , altitude:true, success: function(obj){ that.setData({ userLocalInfo:"经度:"+obj.latitude+"\n纬度:" }) } }) } })