【问题标题】:Vue.js - multiple condition class bindingVue.js - 多条件类绑定
【发布时间】:2020-12-02 00:28:00
【问题描述】:

根据多个条件将类绑定到标签的正确方法是什么?

鉴于此标签,似乎在尝试编写多个条件时,一个被另一个覆盖。

<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>

因此,在上面的示例中,text-bold 类被 bg-white text-black 覆盖,因为第二个条件覆盖了第一个类绑定。

有没有办法在 vue 类绑定中以if, else if, else 样式构造条件?

【问题讨论】:

    标签: javascript vue.js vuejs2 vue-component quasar


    【解决方案1】:

    将该类属性绑定到名为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'
          } 
    
       }
    
    }
    

    【讨论】:

    • 这种说法是有道理的,但实际上是行不通的。我收到错误TypeError: Cannot read property 'row' of undefined
    • 我不确定应该如何定义props,因为它已传递给那里的组件
    猜你喜欢
    • 2017-08-29
    • 1970-01-01
    • 2018-04-23
    • 1970-01-01
    • 2020-02-13
    • 1970-01-01
    • 2013-09-04
    • 2015-08-07
    • 2021-08-10
    相关资源
    最近更新 更多