uniapp使用getCurrentPages调用首页方法 再次对数据进行赋值会发现不生效

   getLocationMethods() {
        const pages = getCurrentPages(); // 获取当前页面栈数组
        const firstPage = pages[0]; // 获取首页实例
        firstPage.properties.getLocation() //直接引用方法给首页数据重新赋值会不生效
        // 修改指针转向再调用
        const currentInstance = firstPage.$vm; //更改跳转后this的指向
        if (currentInstance && currentInstance.getLocation) {
          currentInstance.getLocation(); // 调用首页的方法
        }
      },

上述代码在app.vue

 getLocation() {
        uni.getLocation({
          type: 'wgs84',
          success: (res) => {
            console.log('当前位置的经度:' + res.longitude);
            console.log('当前位置的纬度:' + res.latitude);
            // 根据获取的经纬度信息根据后端api得出具体的区域信息
            const longitude = res.longitude
            const latitude = res.latitude
            // 把定位信息存进缓存
            uni.setStorageSync('longitude', longitude);
            uni.setStorageSync('latitude', latitude);
            getAreaQueryByLogLat({
              lat: latitude,
              log: longitude
            }).then(res => {
              this.cityInfo.district.name = res.data.district.name
              this.cityInfo.district.id = res.data.district.code
              this.cityInfo.city.name = res.data.city.name
              this.cityInfo.city.id = res.data.city.code
              console.log(this.cityInfo)
            })
          },
          fail(err) {
            console.error('获取位置失败:', err);
          }
        });
         uni.setStorageSync('cityInfo', this.cityInfo);
      },

上述代码在index.vue

调用后会出现res的数据无法赋值到this.xxx上

使用$set会报找不到$set

然后就分别打印两边的this出来发现果然不一致

更改this的指向最后就好了

  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值