# 自定义组件

vue自定义组件v-model绑定数据的实现方法

代码

父组件调用

1
<list v-model="data" :multiple="true"></list>

子组件list.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
<template>
<el-select v-model="val" v-bind="$attrs" placeholder="请选择">
<el-option
v-for="item in list"
:key="item.id"
:label="item.name"
:value="item.id">
</el-option>
</el-select>
</template>

<script>
import { getColumnList } from '@/api/column.js'

export default {
inheritAttrs: false,
model: {
prop: 'value',
event: 'change'
},
props: {
value: null
},
data () {
return {
list: [{id:'1',name:'张三'},{id:'2',name:'李四'}]
}
},
computed: {
val: {
get () {
return this.value
},
set (newval) {
this.$emit('change', newval)
}
}
}
}
</script>