微信小程序的wxml或模板的wxml中如何執行復雜運算的方法

前言:

在微信小程序的wxml中可以在{{}}進行裏進行簡單的三目運算。但是複雜的parseInt()、str.split()、num.toFixed()等在{{}}裏是無效的。本文旨在解決這個問題

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)
    }
  }
</wxs>
<view>
 <view class='num'>{{amount>10000?fn.parseInt(amount/10000):amount}}</view>
  <text class='text'>消費額({{amount>10000?"萬元":"元"}})</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>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章