简体   繁体   English

如何观察聚合物3的性质变化?

[英]How do I observe property changes in Polymer 3?

How do I implement continuous checking (time based) of a property-change in a component using an event listener in Polymer 3? 如何使用Polymer 3中的事件监听器对组件中的属性更改进行连续检查(基于时间)?

These are my component's properties: 这些是我组件的属性:

static get properties() {
  return {
    longitude: {
      type: Number
    },
    latitude: {
      type: Number
    },
    accuracy: {
      type: Number
    }
  };
}

You could use a complex observer that is called whenever any of the specified properties change. 您可以使用一个复杂的观察器 ,只要任何指定的属性发生变化,它都会被调用。 To do this, declare an observers getter that returns a string array, where each string is the observer method name, followed by a list of dependencies (ie, properties to be observed) in parentheses: 为此,声明一个observers getter,它返回一个字符串数组,其中每个字符串都是观察者方法名称,其后是括号中的依赖项列表(即要观察的属性):

static get observers() {
  return ['_onPropsChanged(longitude, latitude, accuracy)'];
}

_onPropsChanged(longitude, latitude, accuracy) {
  console.log({ longitude, latitude, accuracy });
}

demo 演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM