常常遇到动态获取数据后,多种样式来判断字体的颜色,可以使用绑定style或者class,如 :style=“{ color: textColor, fontSize: item.fontSize + ‘px’ }” , :class=“Bclass(item)” 等 多种方法,所以在此记录一下
// 可以直接使用 item中的参数 如:(item.fontSize)
// 可以使用三元运算来 如 {color:item.Bcolor==1?'red':''}
<div class="imgs-div" v-for="(item,index) in Data" :key="index">
<span :style="{ color: textColor, fontSize: item.fontSize + 'px' }" ></span>
</div>
//在data里面
data() {
return {
textColor: 'red',
};
},
// 在method中
methods: {
textColor(){
return 'red'
}
},
// 在computed中
computed: {
textColor() {
return 'red'
}
}
但是有的时候我想简洁代码,需要在style 或者class 中 传递 item 的参数
// 在style 中绑定方法传递参数
<div class="imgs-div" v-for="(item,index) in Data" :key="index">
<span :style="Bcolor(item)" ></span>
</div>
// 在method中
methods: {
Bcolor(item){
return `color:${item.color}`
}
},
// 在computed中
computed:{
Bcolor() {
return function (item) {
return `color:${item.color}`
}
}
}