我正在使用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?
只是为了确保这个问题显示为已回答:
createRenderRoot
允许您覆盖创建阴影根的操作。它通常用于渲染到dom:
createRenderRoot() {
return this;
}
虽然它可以用来完全渲染其他地方。
我真的建议使用shadow DOM。如果元素覆盖它自己的轻DOM,则组合很困难。