Vue配合Select使用的例子:
<option :value="value" v-for="(text, value) in list.items">{{ text }}</option>
构建列表主要是这行,通过v-for循环从字典items中取值,并创建option下拉列表。
v-model="list.selected"
v-model绑定列表默认值,通过它可以访问当前选中项目的id和value.
<button @click="test">set to 3</button></p>
一个按钮,通过外界途径修改默认值的示例。
- 因为太简单了,就不多说什么,稍懂一点点Vue的朋友一看就明白了。
<template>
<div>
<select v-model="list.selected">
<option :value="value" v-for="(text, value) in list.items">{{
text
}}</option>
</select>
<BR /><BR /> value={{ list.selected }} , text={{ list.items[list.selected] }}
<BR /><BR />
<button @click="list.selected = '3'">set to 3</button>
</div>
</template>
<script>
export default {
name: 'demo',
data() {
return {
list: {
selected: '2',
items: {
'1': 'Apple',
'2': 'Banana',
'3': 'Cherry',
},
},
}
},
methods: {},
mounted: function() {},
}
</script>