Lit学习小结

497 阅读1分钟
  1. 渲染模板

    import { LitElement, html } from 'lit-element';
    
    class MyElement extends LitElement {
    
      // Implement `render` to define a template for your element.
      render(){
        /**
         * Return a lit-html `TemplateResult`.
         *
         * To create a `TemplateResult`, tag a JavaScript template literal
         * with the `html` helper function.
         */
        return html`
          <div>
            <p>A paragraph</p>
          </div>
        `;
      }
    }
    customElements.define('my-element', MyElement);
    
  2. 属性、循环、判断条件

    import { LitElement, html } from 'lit-element';
    
    class MyElement extends LitElement {
      static get properties() {
        return {
          myString: { type: String },
          myArray: { type: Array },
          myBool: { type: Boolean }
        };
      }
      constructor() {
        super();
        this.myString = 'Hello World';
        this.myArray = ['an','array','of','test','data'];
        this.myBool = true;
      }
      render() {
        return html`
          <p>${this.myString}</p>
          <ul>
            ${this.myArray.map(i => html`<li>${i}</li>`)}
          </ul>
          ${this.myBool?
            html`<p>Render some HTML if myBool is true</p>`:
            html`<p>Render some other HTML if myBool is false</p>`}
        `;
      }
    }
    
    customElements.define('my-element', MyElement);
    
  3. 绑定属性值

    import { LitElement, html } from 'lit-element';
    
    class MyElement extends LitElement {
      static get properties() {
        return {
          prop1: String,
          prop2: String,
          prop3: Boolean,
          prop4: String
        };
      }
      constructor() {
        super();
        this.prop1 = 'text binding';
        this.prop2 = 'mydiv';
        this.prop3 = true;
        this.prop4 = 'pie';
      }
      render() {
        return html`
          <!-- text binding -->
          <div>${this.prop1}</div>
    
          <!-- attribute binding -->
          <div id="${this.prop2}">attribute binding</div>
    
          <!-- boolean attribute binding -->
          <div>
            boolean attribute binding
            <input type="text" ?disabled="${this.prop3}"/>
          </div>
    
          <!-- property binding -->
          <div>
            property binding
            <input type="text" .value="${this.prop4}"/>
          </div>
    
          <!-- event handler binding -->
          <div>event handler binding
            <button @click="${this.clickHandler}">click</button>
          </div>
        `;
      }
      clickHandler(e) {
        console.log(e.target);
      }
    }
    
    customElements.define('my-element', MyElement);
    
  4. slot

    import { LitElement, html } from 'lit-element';
    
    class MyElement extends LitElement {
      render(){
        return html`
          <div>
            <slot name="one"></slot>
            <slot name="two"></slot>
          </div>
        `;
      }
    }
    customElements.define('my-element', MyElement);
    
  5. 多模板

    import { LitElement, html } from 'lit-element';
    
    import './my-header.js';
    import './my-article.js';
    import './my-footer.js';
    
    class MyPage extends LitElement {
        render() {
            return html`
            <my-header></my-header>
            <my-article></my-article>
            <my-footer></my-footer>
        `;
        }
    }
    customElements.define('my-page', MyPage);