試過兩個辦法。
第一種:
<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";
}
}
完美解決!