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