爱新觉罗·苟子 2022-04-12 09:40 采纳率: 25%
浏览 3341

Uncaught TypeError: Cannot set properties of undefined (setting 'province') 报错

在开发uniAPP 的时候,在使用 getLocation 这个API 给Vue的data赋值时候报这个错误

源代码如下
getLocation(){
let _this = this
uni.getLocation({
type: 'wgs84',
success: function (res) {
var point = new _this.BMap.Point(res.longitude,res.latitude);
var gc = new _this.BMap.Geocoder();
gc.getLocation(point, function(rs){
rs = JSON.parse(JSON.stringify(rs))
this.province = rs.addressComponents.province;
this.city = rs.addressComponents.city;
this.county = rs.addressComponents.district || rs.addressComponents.city;
});
},
fail: function (res) {
console.log(res);
},
});
},

浏览器报错 Uncaught TypeError: Cannot set properties of undefined (setting 'province')

看了下 这是this指向的问题 重新定义this 就可以了

改后的代码

getLocation(){
let _this = this
uni.getLocation({
type: 'wgs84',
success: function (res) {
var point = new _this.BMap.Point(res.longitude,res.latitude);
var gc = new _this.BMap.Geocoder();
gc.getLocation(point, function(rs){
rs = JSON.parse(JSON.stringify(rs))
_this.province = rs.addressComponents.province;
_this.city = rs.addressComponents.city;
_this.county = rs.addressComponents.district || rs.addressComponents.city;
});
},
fail: function (res) {
console.log(res);
},
});
},

另需要在Vue项目中获取定位地址信息的,可以参照我的这篇文章

https://blog.csdn.net/AisinGioroGouzi/article/details/124105722

  • 写回答

1条回答 默认 最新

  • 雾里桃花 2022-04-12 09:48
    关注

    rs.addressComponents.province; 你输出一下这个地方有值吗?

    评论

报告相同问题?

问题事件

  • 创建了问题 4月12日

悬赏问题

  • ¥20 有偿:在ubuntu上安装arduino以及其常用库文件。
  • ¥15 请问用arcgis处理一些数据和图形,通常里面有一个根据点划泰森多边形的命令,直接划的弊端是只能执行一个完整的边界,但是我们有时候会用到需要在有很多边界内利用点来执行划泰森多边形的命令
  • ¥30 在wave2foam中执行setWaveField时遇到了如下的浮点异常问题,请问该如何解决呢?
  • ¥20 看图片)删除这个自动化录屏脚本就一直报错找不到脚本文件,如何解决?(相关搜索:bat文件)
  • ¥750 关于一道数论方面的问题,求解答!(关键词-数学方法)
  • ¥200 csgo2的viewmatrix值是否还有别的获取方式
  • ¥15 Stable Diffusion,用Ebsynth utility在视频选帧图重绘,第一步报错,蒙版和帧图没法生成,怎么处理啊
  • ¥15 请把下列每一行代码完整地读懂并注释出来
  • ¥15 pycharm运行main文件,显示没有conda环境
  • ¥15 寻找公式识别开发,自动识别整页文档、图像公式的软件