在属性值更改事件上配置观察器的推荐方法。
当前使用更新的方法并循环通过changedProperties参数,该方法可以正常工作。
static get properties() {
return {
testprop: { type:String },
};
updated(changedProperties) {
changedProperties.forEach((oldValue, propName) => {
if (propName=='testprop') {
console.log('testprop CHANGED!');
this.dosomething();
}
});
}但与聚合物2.0相比,似乎过于复杂:
testprop: {
type:String,
value: '',
observer: 'dosomething',
},为了观察属性事件的变化并运行一些代码,有没有更简单/推荐的方法呢?
发布于 2019-05-12 22:24:08
根据Alan的建议:
set testprop(val) {
let oldVal = this._testprop;
this._testprop = val;
this.requestUpdate('testprop', oldVal);
this.dosomething();
}
get testprop() { return this._testprop; }https://stackoverflow.com/questions/56047198
复制相似问题