gpt4 book ai didi

javascript - lit-element 属性观察者事件变化

转载 作者:行者123 更新时间:2023-12-02 23:41:34 32 4
gpt4 key购买 nike

在属性值更改事件上配置观察者的推荐方法。

当前使用更新的方法并循环更改有效的Properties参数。

static get properties() {
return {
testprop: { type:String },
};
updated(changedProperties) {
changedProperties.forEach((oldValue, propName) => {
if (propName=='testprop') {
console.log('testprop CHANGED!');
this.dosomething();
}
});
}

但与 Polymer 2.0 相比似乎过于复杂:

testprop: {
type:String,
value: '',
observer: 'dosomething',
},

要观察属性事件更改并运行一些代码,是否有更简单/推荐的方法?

最佳答案

艾伦的建议:

set testprop(val) {
let oldVal = this._testprop;
this._testprop = val;
this.requestUpdate('testprop', oldVal);
this.dosomething();
}
get testprop() { return this._testprop; }

关于javascript - lit-element 属性观察者事件变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56047198/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com