2 回答

TA贡献1799条经验 获得超9个赞
我认为,在onChange
函数计时冲突上渲染时间和选定的属性定义。因此,最好先分配一个setTimeout
,onChange
然后它才能正常工作。在下面的链接示例中。删除时,我setTimeout
也面临同样的问题。此外,您无需onChange
在属性中声明为函数。
static get properties() {
return {
options: { type: Array },
selected: { type: String }
};
}
constructor() {
super();
this.options = [{value:1, text:"ben"},{value:2, text:"sen"},{value:3, text:"oo"},{value:4, text:"biz"},{value:5, text:"siz"},{value:6, text:"onlar"}];
this.selected = 3
}
render() {
return html`
<select id="sel" @change="${this.onChange}">
${this.options.map(option => html`
<option value="${option.value}" ?selected=${this.selected === option.value}>${option.text}</option>
`)}
</select>
<button @click="${this.changeSelected}">Random chage selected option</button>
`;
}
onChange(x){
setTimeout(()=>{
this.selected = this.shadowRoot.querySelector('#sel').value
console.log('Selected -->', this.selected );
},300)
}
changeSelected(){
this.selected = (this.options[Math.floor(Math.random() * 6)].value)
console.log(this.selected)
}
添加回答
举报