官方推薦picker,但是我們項目用picker還要搭配Popup和cell、field,維護太太麻煩,所以自己封裝一個
select單選
//封裝VanFieldSelectPicker組件 <template> <div> <van-field v-model="resultLabel" v-bind="$attrs" readonly is-link input-align="right" @click="show = !show" /> <van-popup v-model="show" position="bottom"> <van-picker v-bind="$attrs" :columns="columns" show-toolbar @cancel="cancel" @confirm="onConfirm" @change="change" :value-key="this.option.label" /> </van-popup> </div> </template> <script> export default { name: 'VanFieldSelectPicker', model: { prop: 'selectValue' }, props: { columns: { type: Array, default: function () { return [] } }, selectValue: { type: [String, Number], default: '' }, option: { type: Object, default: function () { return { label: 'label', value: 'value' } } } }, computed: { resultLabel: {//雙向綁定的數據做修改需要用get/set get () { const res = this.columns.filter(item => { return item[this.option.value] === this.resultValue }) return res.length ? res[0][this.option.label] : '' }, set () { } } }, data () { return { show: false,//Popup是否彈出 resultValue: this.selectValue //初始選中數據 } }, methods: { onConfirm (value, index) {//確定 this.resultValue = value[this.option.value] this.show = !this.show this.$emit('confirm', value, index, this.resultValue) }, change (val, index) {//當數據改變 this.$emit('change',