在微信小程序的wxml中可以在{{}}进行里进行简单的三目运算。但是复杂的比如字符串截取,parseInt()、str.split()、num.toFixed(),substring等在{{}}里是无效的。本文旨在解决这个问题
1.在wxml中执行复杂运算
<wxs module="fn">
module.exports = {
parseInt:function(num){
return "约"+parseInt(num)
},
split:function(str){
return str.split(",")
},
toFixed:function(num){
return (num).toFixed(2)
}
},
substrLast: function (str) {//字符串截取
return str.substring(str.indexOf("-") + 1);
},
substrFirst: function (str) {
return str.substring(0,str.indexOf("-"));
}
</wxs>
<view>
<view class='num'>{{amount>10000?fn.parseInt(amount/10000):amount}}</view>
<text class='text'>消费额({{amount>10000?"万元":"元"}})</text>
<text class='text'>{{fn.substrFirst(122+"-"+ffa)}}</text>
</view>
2.在模板的wxml中执行复杂运算
<template name="ordersTmp">
<view class="ordersTmpBox">
<view class="detail"> 消费额:{{fn.parseInt(amount/10000)}}万元</view>
</view>
<wxs module="fn">
module.exports = {
parseInt:function(num){
return "约"+parseInt(num)
},
split:function(str){
return str.split(",")
},
toFixed:function(num){
return (num).toFixed(2)
}
}
</wxs>
</template>