这篇文章主要介绍了一个基于iview的一个下拉封装组件。

文件夹路径

1
2
3
4
components
-- live-select
---- live-select.vue
---- index.js

live-select.vue

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<template>
<Select ref="LiveSelectRef" :value="curVal" @on-change="handleChange" :placeholder="placeholder" :class="clz"
:clearable="curClr" :disabled="curDisabled" transfer :multiple="curMultiple" filterable
:max-tag-count="maxTagCount">
<Option v-if="showAll" value="">全部</Option>
<Option v-for="o in curOption" :value="o.value" :key="`live-${o.value}`">{{o.label}}</Option>
</Select>
</template>

<script>
import {Select, Option} from 'view-design';

export default {
name: 'LiveSelect',
components: {
Select,
Option
},
model: {
prop: 'value',
event: 'change'
},
props: {
value: {
type: [String, Number, Array],
default: ''
},
placeholder: {
type: String
},
clz: { // class类名
type: String
},
disabled: { // 不可选择
type: Boolean,
default: false
},
clearable: { // 是否可清空
type: Boolean,
default: false
},
filter: { // 过滤包含 [value]
type: Array,
default: () => {
return []
}
},
filterKill: { // 反过滤除去 [value]
type: Array,
default: () => {
return []
}
},
multiple: { // 是否支持多选
type: Boolean,
default: false
},
showAll: { // 是否显示“全部”
type: Boolean,
default: false
},
maxTagCount: { // 最大显示的 tag 文本长度
type: Number,
default: 1
},
option: { // 选项数组
type: Array,
default: () => {
return []
}
}
},
computed: {
curOption: function() {
let fArr = this.filter || this.filterKill
if(fArr.length) {
let len = this.filter.length
return this.option.filter((item) => {
return len? fArr.includes(item): !fArr.includes(item)
})
}
return this.option
}
},
data() {
return {
curDisabled: this.disabled,
curClr: this.clearable,
curVal: Array.isArray(this.value) ? this.value : this.value + '',
curMultiple: this.multiple
}
},
watch: {
value(val) {
this.curVal = Array.isArray(this.value) ? val : val + ''
},
disabled(val) {
this.curDisabled = val
},
clearable(val) {
this.curClr = val
},
filter(val) {
this.filter = val
},
filterKill(val) {
this.filterKill = val
}
},
methods: {
handleChange(selected) {
this.$emit('change', selected)
this.$emit('on-change', selected)
}
}
}
</script>

index.js

1
2
3
// 暴露组件
import LiveSelect from './live-select.vue'
export default LiveSelect