Vue中如何实现基于三元运算符的动态样式切换关联?

更新于
2026-09-23 23:57:28
1阅读来源:SEO教程
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计351个文字,预计阅读时间需要2分钟。

Vue中如何实现基于三元运算符的动态样式切换关联?

假设需求:后端返回状态码1:启用,0:禁用,1:使用启用为绿色,其他不添加样式。

Vue中如何实现基于三元运算符的动态样式切换关联?

直接输出结果:el-table-column prop=statusName align=center label=状态 template slot-scope=scope> {{ scope.row.status===1 ? '启用' : '禁用' }}

假设有需求:

后台返回状态1:启用,0:禁用

1、若要使启用为绿色,禁用不添加其他样式

<el-table-column prop="statusName" align="center" label="状态"> <template slot-scope="scope"> <div :class="{active:scope.row.status==1}"> {{ scope.row.statusName }} </div> </template> </el-table-column> .active{ color:green; }

1、若要使启用为绿色,禁用为红色,可使用三元表达式绑定样式

<el-table-column prop="statusName" align="center" label="状态"> <template slot-scope="scope"> <div :class="scope.row.status==1? 'active':'native'"> {{ scope.row.statusName }} </div> </template> </el-table-column> .active{ color:green; } .native{ color:red; }

补充知识:vue通过判断写样式(v-bind)

如下所示:

v-bind:style="$index % 2 > 0?'background-color:#FFF;':'background-color:#D4EAFA;'"

以上这篇Vue 样式切换及三元判断样式关联操作就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持易盾网络。

本文共计351个文字,预计阅读时间需要2分钟。

Vue中如何实现基于三元运算符的动态样式切换关联?

假设需求:后端返回状态码1:启用,0:禁用,1:使用启用为绿色,其他不添加样式。

Vue中如何实现基于三元运算符的动态样式切换关联?

直接输出结果:el-table-column prop=statusName align=center label=状态 template slot-scope=scope> {{ scope.row.status===1 ? '启用' : '禁用' }}

假设有需求:

后台返回状态1:启用,0:禁用

1、若要使启用为绿色,禁用不添加其他样式

<el-table-column prop="statusName" align="center" label="状态"> <template slot-scope="scope"> <div :class="{active:scope.row.status==1}"> {{ scope.row.statusName }} </div> </template> </el-table-column> .active{ color:green; }

1、若要使启用为绿色,禁用为红色,可使用三元表达式绑定样式

<el-table-column prop="statusName" align="center" label="状态"> <template slot-scope="scope"> <div :class="scope.row.status==1? 'active':'native'"> {{ scope.row.statusName }} </div> </template> </el-table-column> .active{ color:green; } .native{ color:red; }

补充知识:vue通过判断写样式(v-bind)

如下所示:

v-bind:style="$index % 2 > 0?'background-color:#FFF;':'background-color:#D4EAFA;'"

以上这篇Vue 样式切换及三元判断样式关联操作就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持易盾网络。