vue 之 render 封装一个select组件
父组件
value - {{ value }} ; value2 - {{ value2 }}
<!-- disabled clearable -->
<Select v-model="value" :option-data="optionData" placeholder="请选择" clearable />
<Select v-model="value2" :option-data="optionData" />
value: '',
value2: 'area1',
optionData: [
{ label: '区域1', value: 'area1' },
{ label: '区域2', value: 'area2' }
],
Select.vue
<script>
export default {
name: 'Select',
props: {
value: {
default: '',
type: [String, Number]
},
optionData: {
default: () => {
return []
},
type: Array
}
},
computed: {
newValue: {
get({ value }) {
return value
},
set(val) {
this.$emit('input', val)
}
}
},
methods: {
onChangeHandle(val) {
this.newValue = val
}
},
render(createElement) {
return createElement(
'el-select',
{
attrs: {
...this.$attrs
},
props: {
value: this.newValue
},
on: {
change: this.onChangeHandle
}
},
this.optionData && this.optionData.map(option => {
return createElement(
'el-option',
{
props: {
label: option.label,
value: option.value
}
}
)
})
)
}
}
</script>
效果