我用 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,selected和onChange作为属性。在第一次渲染时,一切正常。渲染所有选项,所选值反映在选择中。但是,如果我更改selected它似乎不会更新所选选项。如果我使用 …