简体   繁体   English

LitElement 替换组件内的内容

[英]LitElement replace content inside component

So I want to have a loading indicator inside my lit component until it has loaded.所以我想在我的发光组件中有一个加载指示器,直到它加载。 This is the HTML:这是 HTML:

            <login-element class=" flex justify-center items-center">
                <div class="animate-spin rounded-full h-11 w-11 border-t-2 border-b-2 border-purple-500"></div>
            </login-element>

Here is the typescript code:这是打字稿代码:

import {LitElement, html} from 'lit';
import {customElement} from 'lit/decorators.js';

@customElement('login-element')
export class LoginElement extends LitElement {

    override createRenderRoot(){ return this; }

    override render() {
        return html`<a href="/auth0" class="inline-block text-sm px-4 py-2 leading-none border rounded text-white border-white hover:border-transparent hover:text-teal-500 hover:bg-white mt-4 lg:mt-0">Sign In</a>`
    }
}

Currently, it appends the sign-in button.目前,它附加了登录按钮。 I want it to replace the loading indicator.我希望它替换加载指示器。 Is there a recommended way of doing it?有推荐的方法吗?

You can pass the HTML for the Spinner and the info, whether something is loading via the custom element.您可以传递 Spinner 的 HTML 和信息,无论是否通过自定义元素加载。

<login-element loading><b>Spinner-Element</b></login-element>

The spinner-HTML itself you'll get via您将获得的微调 HTML 本身

unsafeHTML(this.innerHTML)

inside your custom-element.在您的自定义元素中。

Then you can use your spinner and decide showing it with a conditional inside the render() function.然后,您可以使用微调器并决定在 render() 函数内使用条件来显示它。

  @property({ type: Boolean })
  loading = false;
  
  render() {
    return html`     
      ${this.loading ? html`
      Showing spinner: ${unsafeHTML(this.innerHTML)}`: 'loaded!'}
    `;
  }

In this working example, if you remove the "loading" attribute, you'll see the "loaded!"在这个工作示例中,如果您删除“loading”属性,您将看到“loaded!” message, otherwise you'll see what you passed into the element as spinner-markup: https://stackblitz.com/edit/lit-element-typescript-starter-pl3bij?file=my-element.ts消息,否则你会看到你作为微调标记传递给元素的内容: https : //stackblitz.com/edit/lit-element-typescript-starter-pl3bij? file = my-element.ts

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM