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";
     }
}

完美解决!

posted on 2022-02-28 17:05  northwest  阅读(1458)  评论(0编辑  收藏  举报

导航