ElementUI下拉框 文字变多时下拉框也变宽

官网示例

先把官网代码拷贝到项目中,查看效果

<template>
<div class="d1">
<el-select v-model="value" placeholder="请选择">
<el-option
v-for="item in options"
:key="item.value"
:label="item.label"
:value="item.value">
</el-option>
</el-select>
</div>
</template>

<script>
export default {
name: "VideoView",
data(){
return {
options: [{
value: '选项1',
label: '黄金糕黄金糕黄金糕黄金糕黄金糕黄金糕黄金糕黄金糕黄金糕黄金糕黄金糕黄金糕'
}, {
value: '选项2',
label: '双皮奶'
}, {
value: '选项3',
label: '蚵仔煎'
}, {
value: '选项4',
label: '龙须面'
}, {
value: '选项5',
label: '北京烤鸭'
}],
value:''
}
}
}
</script>

<style scoped>
.d1 {
margin-left: 20px;
}
</style>

Kapture 2022-10-03 at 16.27.05

实现

<template>
<div class="d1">
<el-select class="autose" v-model="value" placeholder="请选择">
<template slot="prefix">
{{(options.find(items=>items.value===value) || {} ).label}}
</template>
<el-option
v-for="item in options"
:key="item.value"
:label="item.label"
:value="item.value">
</el-option>
</el-select>
</div>
</template>

<script>
export default {
name: "VideoView",
data(){
return {
options: [{
value: '选项1',
label: '黄金糕黄金糕黄金糕黄金糕黄金糕黄金糕黄金糕黄金糕黄金糕黄金糕黄金糕黄金糕'
}, {
value: '选项2',
label: '双皮奶'
}, {
value: '选项3',
label: '蚵仔煎'
}, {
value: '选项4',
label: '龙须面'
}, {
value: '选项5',
label: '北京烤鸭'
}],
value:''
}
}
}
</script>

<style scoped>
.d1 {
margin-left: 20px;
}
.autose{
min-width: 140px;
}
.autose >>> .el-input__prefix{
position: relative;
padding: 0 30px;
box-sizing: border-box;
border: 1px solid #ffffff00;
height: 40px;
line-height: 40px;
left: 0;
visibility: hidden;
}
.autose >>> input{
position: absolute;
}
</style>

效果

Kapture 2022-10-03 at 17.35.21