简体   繁体   中英

Vue.js - multiple condition class binding

What is the proper way to bind class to a tag based on multiple conditions?

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.

Is there a way to structure conditions in if, else if, else style in vue class binding?

Bind that class attribute to a computed property called 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'
      } 

   }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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