gpt4 book ai didi

javascript - 如何在没有 Shadow DOM 的情况下创建 LitElement?

转载 作者:行者123 更新时间:2023-12-01 14:50:54 25 4
gpt4 key购买 nike

我正在使用 LitElement 创建一个 Web 组件。这是来自 https://lit-element.polymer-project.org/guide/start

// Import the LitElement base class and html helper function
import { LitElement, html } from 'lit-element';

// Extend the LitElement base class
class MyElement extends LitElement {

/**
* Implement `render` to define a template for your element.
*
* You must provide an implementation of `render` for any element
* that uses LitElement as a base class.
*/
render(){
/**
* `render` must return a lit-html `TemplateResult`.
*
* To create a `TemplateResult`, tag a JavaScript template literal
* with the `html` helper function:
*/
return html`
<!-- template content -->
<p>A paragraph</p>
`;
}
}
// Register the new element with the browser.
customElements.define('my-element', MyElement);

如何在没有 Shadow DOM 的情况下创建 LitElement?

我想在没有#shadow-root 的情况下在这里创建它:
enter image description here

最佳答案

只是为了确保这个问题显示为已回答:createRenderRoot允许您覆盖创建影子根的操作。它通常用于渲染到 light dom:

createRenderRoot() {
return this;
}
虽然它可以用来完全渲染其他地方。
我真的推荐使用影子 DOM。如果一个元素覆盖了它自己的轻量级 DOM,那么组合就很困难。

关于javascript - 如何在没有 Shadow DOM 的情况下创建 LitElement?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55126694/

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