LitElement:从 attributeChangedCallback() 调用 requestUpdate() 不会更新渲染

问题描述

当我在更改 classMap 后从 this.requestUpdate()调用 attributeChangedCallback(...) 时,不会调用渲染函数

当我超时调用它时,它似乎确实有效。这是这样做的方式还是这是一个错误

attributeChangedCallback(name: string,oldVal: AttributeType,newVal: AttributeType) {
    super.attributeChangedCallback(name,oldVal,newVal);

  ...
  
  this.myClassMap = {
    ...this.myClassMap,foo: newValueBasedOnChangedProperty,}

  // this doesn't seem to do anything
  this.requestUpdate(); 
  
  // this does trigger a re-render
  setTimeout(() => this.requestUpdate(),0); 
}

似乎也可以使用此方法等待 updateComplete 承诺:

this.updateComplete.then(
  () => this.requestUpdate()
);

但我仍然觉得我把车放在马之前。

解决方法

我上面描述的行为似乎与结合反射的属性变化有关。有关详细信息,请参阅 LitElement GitHub 存储库上的此 issue

,

您应该使用 await element.updateComplete.then(() => this.requestUpdate());