1.给select设置一个key,options数据更新后刷新key——解决无法更新显示label问题
2.设置remote、filterable为true,动态更新options下拉列表。此时不要更新key(避免下拉框自动收起),在visible-change事件里面监听visible=false时再更更新key
<el-select v-model="value" placeholder="请选择" :key="selectKey" :remote="true" filterable @visible-change="visibleChange">
<el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value"></el-option>
</el-select>
data() {
return {
options: [],
value: '',
selectKey: 1
};
},
mounted() {
setTimeout(() => {
this.value = '选项1';
this.options = [
{value: '选项1', label: '黄金糕'},
{value: '选项2', label: '双皮奶'}
];
this.selectKey++;
}, 5000);
},
methods: {
visibleChange(visible) {
if (!visible) {
this.selectKey++;
} else {
setTimeout(() => {
// 更新下拉框
this.options = [
{value: '选项1', label: '黄金糕'},
{value: '选项2', label: '双皮奶'},
{value: '选项3', label: '莲子粥'},
{value: '选项4', label: '豆沙包'},
];
}, 2000);
}
}
}
};