uniapp: 动态绑定/加载/切换元素class的一种有效姿势


试过两个办法。

第一种:

<view :class="(a>b)?classa:classb"></view>

但是这种方法报错了。
报错信息:

Property or method "xxx" is not defined on the instance but referenced during render.Make sure that this property is reactive, either in the data option, or for class-based components, by initializing the property.

说我的class还没有定义。

正确姿势

在class里面写个方法,方法返回这个class名。

1.页面:

<view :class="setClass(a,b)"></view>

方法里面可以传参。

2.在method里写这个方法

setClass:function(a,b){
  if(a>b){
    return "classa";
   }else {
    return "classb";
     }
}

完美解决!


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM