渲染数组给el-radio-button替换选中图片
时间: 2023-08-06 11:10:09 浏览: 29
你可以使用类似于上一个示例代码的方法来渲染一个选项数组,并替换每个选项的选中图片。以下是一个示例代码:
```html
<template>
<el-radio-group v-model="selected">
<el-radio-button v-for="(option, index) in options" :key="index" :label="option.value" class="radio-button-with-image">
<img :src="getSelectedImage(option)" :alt="option.label" class="selected-image">
<span>{{ option.label }}</span>
</el-radio-button>
</el-radio-group>
</template>
<script>
export default {
data() {
return {
options: [
{ value: '1', label: 'Option 1', images: { normal: './normal1.png', selected: './selected1.png' } },
{ value: '2', label: 'Option 2', images: { normal: './normal2.png', selected: './selected2.png' } },
{ value: '3', label: 'Option 3', images: { normal: './normal3.png', selected: './selected3.png' } },
],
selected: '1',
};
},
methods: {
getSelectedImage(option) {
return option.value === this.selected ? option.images.selected : option.images.normal;
},
},
};
</script>
<style>
.radio-button-with-image .el-radio__label {
display: flex;
align-items: center;
}
.radio-button-with-image .selected-image {
width: 16px;
height: 16px;
margin-right: 8px;
/* 设置选中图片样式 */
}
</style>
```
在这个例子中,我们在每个选项对象中定义了一个`images`属性,其中包含了每个选项的正常状态和选中状态下的图片URL。我们在`getSelectedImage`方法中根据当前选中的值来返回对应选项的选中或正常状态的图片URL。然后,在`<img>`元素中,我们使用`:src`和`:alt`来动态设置选中或正常状态的图片URL和提示文本。
注意,以上代码中的图片路径需要根据实际情况进行调整。