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)
}
添加回答
舉報