經(jīng)常在下拉框選項(xiàng)中遇到傳入數(shù)據(jù)為‘0’或‘1’技矮,或者‘true’或‘false’又或者是其他英文縮寫的情況抖誉。
<el-form-item label="是否最新:">
<el-select v-model="VersionForm.newest" placeholder="請選擇...">
<el-option
v-for="newest in NewestOptions"
:key="newest.key"
:label="newest.display_name"
:value="newest.key"
/>
</el-select>
</el-form-item>
定義一個選項(xiàng)的數(shù)組
const NewestOptions = [
{ key: true, display_name: '是' },
{ key: false, display_name: '否' }
]
const NewestKeyValue = NewestOptions.reduce((acc, cur) => {
acc[cur.key] = cur.display_name
return acc
}, {})
在data中設(shè)置過濾器
filters: {
NewestFilter(newest) {
return NewestKeyValue[newest]
}
},
記得要在data()return{}中定義該變量
data() {
return {
NewestOptions,
}
}