For AI agents: the complete documentation index is available at /llms.txt, the full documentation bundle is available at /llms-full.txt, and this page is available as Markdown at /developers/blog/2021-05-19-lit-2.0.md.

升级到 Lit 2.0

我们将前端升级到使用 Lit 2.0,这是 LitElement(3.0)和 lit-html(2.0)的 major bump,今后将一起在 Lit 名称下发展。

这次升级带来了许多出色的改进,但也包含一些 breaking changes。

如果你开发了 custom card 或 view,并从我们的 components 中使用了 LitElementlit-html,你的 component 将在下一个版本(2021.6)中使用 Lit 2.0。如果你不确定是否从我们的 components 中使用了 LitElement,你的代码看起来可能像这样:

const LitElement = Object.getPrototypeOf(customElements.get("ha-panel-lovelace"));
const html = LitElement.prototype.html;
const css = LitElement.prototype.css;

这不是推荐的做法,我们建议你打包 Lit 到你的 component 中,或从 unpkg.com 或其他来源导入,如这个示例。这样你的 card 就不依赖于 Home Assistant 随附的 Lit 版本。

其中一项变更是,shadowRoot 的创建不再在 constructor 中进行,而是在第一次 update 之前进行。这意味着如果你直接与 DOM 交互(如使用 query selector),你不能假设 shadowRoot 总是可用的。

所有变更请查看 Lit 文档中的升级指南

我们预计大多数 cards 都能无问题地与 Lit 2.0 配合工作,但请 custom card 开发者确保兼容性。你可以使用当前 dev 版本的 Home Assistant 或 nightly 版本的 Home Assistant 来完成这一点,两者目前都使用 Lit 2.0。