简体   繁体   English

Vue.js - 多条件 class 绑定

[英]Vue.js - multiple condition class binding

What is the proper way to bind class to a tag based on multiple conditions?根据多个条件将 class 绑定到标签的正确方法是什么?

Given this tag, it seems that when trying to write multiple conditions one is being overwritten by another.鉴于此标签,似乎在尝试编写多个条件时,一个被另一个覆盖。

<q-tr :props="props" 
    :class=["(props.row.Name=='Row Name 1' || props.row.Name=='Row Name 2')?'text-bold':'bg-white text-black', (props.row.Name=='Row Name 3')?'text-green':'bg-white text-black']
>
</q-tr>

So in the above example text-bold class is overwritten by bg-white text-black since the second condition is overriding the first class binding.所以在上面的例子中text-bold class 被bg-white text-black覆盖,因为第二个条件覆盖了第一个 class 绑定。

Is there a way to structure conditions in if, else if, else style in vue class binding?有没有办法在 vue class 绑定中的if, else if, else样式中构造条件?

Bind that class attribute to a computed property called myClass :将该 class 属性绑定到名为myClass的计算属性:

<q-tr
    :class="myClass"
>
</q-tr>
computed:{
   myClass(){
     if(this.props.row.Name=='Row Name 1' ){
         return 'text-bold';
      }
      else if( this.props.row.Name=='Row Name 3'){
         return 'text-green';
      }
      else{
           return 'bg-white text-black'
      } 

   }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM