聚合物:确定何时加载属性?

Polymer: Determining when properties have loaded?

我知道 attached 函数不保证会加载属性。

现在,我一直在使用 computed 依赖属性的函数,但它非常笨重。

我也用过 async,但我发现它不一致且随意(只是选择一个随机时间来延迟)。

我找不到任何关于处理这个问题的正确方法。

您可以使用观察者。

比如你

 properties:{
       someproperty:{type:Number,observer:'change'}
 },
 change:function(){
  //this function called when the property changes.
 }

有关详细信息,请查看 https://www.polymer-project.org/1.0/docs/devguide/properties.html

除了阿隆的回答:如果你想观察几个属性,那么你可以使用这样的东西:

  properties:{
     someproperty1:{
       type: Number,
     }
     someProperty2:{
       type: Number,
     }
  },

  observers: ['change(someproperty1, someproperty2)'],

  change:function(property1, property2){
    //this function called when the property changes.
  },

注意,当使用单个观察者时,它们将按照设置的顺序触发。因此,如果 someproperty1someproperty2 绑定了特定的观察者,那么 someproperty1 绑定的方法将首先执行。

要了解有关观察者的更多信息,请阅读此处: https://www.polymer-project.org/1.0/docs/devguide/observers#multi-property-observers