提出一种使Lit元素反应性能与ECMA 2022私人领域一起使用的方法
最近,我们获得了新的ECMA 2022标准批准,现在有一个语法可以通过前缀#
来支持私有字段。 多年来,我们一直在使用_
常规上,现在存在一些混淆的空间#
和_
。
问题: 如果您现在声明一个私有字段并将其添加到lit element
属性中,则它将行不通。该组件将不会更新使用#
语法声明的属性的更改。
class ExtendedLitElement extends LitElement {
#somePrivateField
static get properties() {
return {
'#somePrivateField': Array,
}
}
}
每当您这样做时:
this.#somePrivateField = [1, 2, 3];
不会更新元素。 有没有办法注册#
将私有字段作为点亮属性?
Recently we got a new ECMA 2022 standard approved and there is now a syntax to support private fields natively by prefixing #
to the name.
For years we have been using _
conventionally and now there is some space for confusion of #
and _
.
The problem:
If you now declare a private field and add it to lit-element
properties, it would not work. The component will not update on change for a property that is declared with #
syntax.
class ExtendedLitElement extends LitElement {
#somePrivateField
static get properties() {
return {
'#somePrivateField': Array,
}
}
}
Whenever you do:
this.#somePrivateField = [1, 2, 3];
An element will not be updated.
Is there a way to register #
prefixed private field as a lit property?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当前,JS类字段,无论是公共还是私有,都不能用于反应性属性。这是由于实例上定义的类字段,而反应性属性被定义为原型上的登录器。具有类字段掩盖了反应性属性登录器。
请参阅: https://lit.dev/dev/docs/docs/docs/components /properties/#避免级别的阶级景点
您必须继续使用
_
前缀的反应性属性或手动调用this.requequestupdate()
设置了类字段以触发更新。Currently JS class fields, whether public or private, cannot be used for reactive properties. This is due to class fields being defined on the instance whereas reactive properties are defined as accessors on the prototype. Having a class field masks the reactive property accessor.
See: https://lit.dev/docs/components/properties/#avoiding-issues-with-class-fields
You would have to continue using
_
prefixed reactive properties or manually invokethis.requestUpdate()
after setting a class field to trigger an update.