用vue3和elementPlus写一个下拉多选并且支持可输入的组件
时间: 2023-05-13 22:05:41 浏览: 132
好的,以下是用 Vue3 和 Element Plus 写一个下拉多选并且支持可输入的组件的代码:
```
<template>
<el-select
v-model="selectedOptions"
multiple
filterable
remote
reserve-keyword
:remote-method="remoteMethod"
:loading="loading"
:popper-append-to-body="false"
:default-first-option="false"
:collapse-tags="true"
placeholder="请选择"
>
<el-option
v-for="item in options"
:key="item.value"
:label="item.label"
:value="item.value"
></el-option>
</el-select>
</template>
<script>
import { ref } from 'vue';
import { ElSelect } from 'element-plus';
export default {
name: 'MultiSelect',
components: { ElSelect },
props: {
options: {
type: Array,
default: () => [],
},
},
setup(props) {
const selectedOptions = ref([]);
const loading = ref(false);
const remoteMethod = (query) => {
loading.value = true;
setTimeout(() => {
loading.value = false;
const results = props.options.filter((option) =>
option.label.toLowerCase().includes(query.toLowerCase())
);
results.length > 0 ? results : [{ label: query, value: query }];
}, 200);
};
return {
selectedOptions,
loading,
remoteMethod,
};
},
};
</script>
```
希望这个代码能够帮到你!
阅读全文