【问题标题】:Why vue compnent property doesn;t update When open it secodnd time?为什么第二次打开vue组件属性不更新?
【发布时间】:2021-11-17 02:52:45
【问题描述】:

我有 vue 组件,它的 data() 属性 center2 基本上是 lng,lat 对象。此属性应该在每个位置更改时更新。但是当我打开组件时它第一次得到更新。对于下一次尝试,它只显示我分配的默认值。

数据属性

data()
{
return {
     center2: { lat: 0, lng: 0 },
}
}

这是在位置观察器中使用位置更新它的方式

  this.center2.lat = Number(location.latitude);
       this.center2.lng = Number(location.longitude);
       console.log('MY',this.center2);

我可以在控制台中看到 lat long 进展顺利

是什么原因?

【问题讨论】:

  • 你能用你的代码发布一个沙盒示例吗?
  • 你在使用观察者吗?您是否正在寻找 center2 对象的更新?
  • 您的数据不正确。是不是应该返回一个包含center2作为属性的对象
  • 你能举个例子吗?
  • 是的@KurianBenoy 类似的东西

标签: javascript vue.js vuejs3


【解决方案1】:

在 Vue2 中,您可能想尝试在 data() 中返回 center2。如下所示:

data() {
  return {
    center2: {
      lat: 0,
      lng: 0
    }
  }
}

那么center2应该会随着watcher正常更新。

【讨论】:

    猜你喜欢
    • 2021-02-02
    • 1970-01-01
    • 1970-01-01
    • 2014-07-20
    • 2020-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-18
    相关资源
    最近更新 更多