vue v-for 循环中 绑定 style 传递参数 方法 在computed 中使用

常常遇到动态获取数据后,多种样式来判断字体的颜色,可以使用绑定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}`
 		} 
 	} 
 }  
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值