iview 官網(wǎng)示例代碼:
<template>
<Select v-model="model1" style="width:200px">
<Option v-for="item in cityList" :value="item.value" :key="item.value">{{ item.label }}</Option>
</Select>
</template>
<script>
export default {
data () {
return {
cityList: [
{
value: 'New York',
label: 'New York'
},
{
value: 'London',
label: 'London'
},
{
value: 'Sydney',
label: 'Sydney'
},
{
value: 'Ottawa',
label: 'Ottawa'
},
{
value: 'Paris',
label: 'Paris'
},
{
value: 'Canberra',
label: 'Canberra'
}
],
model1: ''
}
}
}
</script>
記錄在實(shí)際應(yīng)用中,我遇到的問(wèn)題
1. 代碼如下(選擇城市列表,給后臺(tái)傳入 value 值查詢對(duì)應(yīng)城市信息,但是如果傳入的是空,返回所有城市信息,結(jié)果是空的時(shí)候,點(diǎn)擊 select 會(huì)有一拍顯示空白,最后解決方案是 value 修改為 0,調(diào)用接口的時(shí)候判斷是 0 就傳入空):
<template>
<Select v-model="model1" style="width:200px">
<Option v-for="item in cityList" :value="item.value" :key="item.value">{{ item.label }}</Option>
</Select>
</template>
<script>
export default {
data () {
return {
cityList: [
{
value: '', //這里改為 value:'0'
label: 'all'
},
{
value: '1',
label: 'A'
},
{
value: '2',
label: 'B'
},
{
value: '3',
label: 'C'
}],
model1: ''
}
}
}
</script>
2. 優(yōu)化代碼時(shí),把 {{}} 里面的文字改成了 v-text,結(jié)果導(dǎo)致點(diǎn)擊的 select 時(shí)候,需要點(diǎn)擊兩次才可以選中,改回去就解決了這個(gè)問(wèn)題。
<template>
<Select v-model="model1" style="width:200px">
<Option v-for="item in cityList" :value="item.value" :key="item.value" v-text=' item.label '></Option>
</Select>
</template>
<script>
export default {
data () {
return {
cityList: [
{
value: '0',
label: 'all'
},
{
value: '1',
label: 'A'
},
{
value: '2',
label: 'B'
},
{
value: '3',
label: 'C'
}],
model1: ''
}
}
}
</script>