小编Cap*_*oot的帖子

自定义 litelement 选择未正确重新渲染

我用 LitElement 创建了一个自定义选择组件:

import { LitElement, html } from 'lit-element';

class CustomSelect extends LitElement {
    static get properties()  {
        return {
            options: { type: Array },
            selected: { type: String },
            onChange: { type: Function }
        };
    }
    constructor() {
        super();
        this.options = [];
    }
    render() {
        return html`
            <select @change="${this.onChange}">
                ${this.options.map(option => html`
                    <option value="${option.value}" ?selected=${this.selected === option.value}>${option.text}</option>
                `)}
            </select>
        `;
    }
    createRenderRoot() {
        return this;
    }
}

customElements.define('custom-select', CustomSelect);
Run Code Online (Sandbox Code Playgroud)

我在创建元素时传入options,selectedonChange作为属性。在第一次渲染时,一切正常。渲染所有选项,所选值反映在选择中。但是,如果我更改selected它似乎不会更新所选选项。如果我使用 …

javascript polymer lit-element lit-html

6
推荐指数
1
解决办法
3201
查看次数

标签 统计

javascript ×1

lit-element ×1

lit-html ×1

polymer ×1