我用 vue-select 创建选择选项。但是,我对 vue-select 的行为有一个问题,当它为空时,它会显示消息"对不起,没有选项匹配"。
网页代码:
<div id="app">
<h1>Vue Select - Custom Labels</h1>
<v-select label="countryName" :options="options"></v-select>
</div>
CSS代码:
body {
font-family: 'Source Sans Pro', 'Helvetica Neue', Arial, sans-serif;
}
h1 {
font-size: 26px;
font-weight: 600;
color: #2c3e5099;
text-rendering: optimizelegibility;
-moz-osx-font-smoothing: grayscale;
-moz-text-size-adjust: none;
}
#app {
max-width: 30em;
margin: 1em auto;
}
JS代码:
Vue.component("v-select", VueSelect.VueSelect);
new Vue({
el: "#app",
data: {
options: null
}
});
https://codepen.io/sagalbot/pen/aEjLPB
制作数据,选项:空
下拉列表将显示消息"抱歉,没有匹配的选项"。单击该选项,下拉菜单选择将挂起。
我希望当选择"对不起,没有选项匹配"时,dropdwon 选择将关闭。
vue-select
允许插槽,我在此代码笔中找到了一个插槽列表
因此,您可以将该槽用于未找到的选项,并在单击该元素时使用 ref
和 $refs
关闭选择。我在此 github 问题中找到了有关如何关闭v-select
<v-select ref="select">
<span slot="no-options" @click="$refs.select.open = false">
Sorry, no matching options
</span>
</v-select>