在Vue中實現dropdownlist功能主要可以借助于Vue組件庫或者自定義組件來實現。以下是一種實現dropdownlist功能的簡單示例:
<template>
<div>
<el-select v-model="value" placeholder="請選擇">
<el-option
v-for="item in options"
:key="item.value"
:label="item.label"
:value="item.value"
></el-option>
</el-select>
</div>
</template>
<script>
export default {
data() {
return {
value: '',
options: [
{ value: 'option1', label: '選項1' },
{ value: 'option2', label: '選項2' },
{ value: 'option3', label: '選項3' },
],
};
},
};
</script>
<template>
<div>
<div @click="toggleDropdown" class="dropdown-toggle">{{ selectedOption.label }}</div>
<ul v-show="isDropdownOpen" class="dropdown-list">
<li v-for="option in options" @click="selectOption(option)" :key="option.value">{{ option.label }}</li>
</ul>
</div>
</template>
<script>
export default {
data() {
return {
isDropdownOpen: false,
selectedOption: { value: '', label: '請選擇' },
options: [
{ value: 'option1', label: '選項1' },
{ value: 'option2', label: '選項2' },
{ value: 'option3', label: '選項3' },
],
};
},
methods: {
toggleDropdown() {
this.isDropdownOpen = !this.isDropdownOpen;
},
selectOption(option) {
this.selectedOption = option;
this.isDropdownOpen = false;
},
},
};
</script>
<style>
.dropdown-list {
display: none;
position: absolute;
background-color: #f9f9f9;
list-style: none;
padding: 0;
margin: 0;
}
.dropdown-toggle {
cursor: pointer;
}
</style>
在實際開發中,可以根據具體需求自定義樣式和功能來實現dropdownlist功能。