PrefaceIn WeChat applet development (native wxml, wxcss), I want to call methods directly in {{ }} to process data, but an error will be reported. For example, when calculating percentages in a project, js floating-point operations may cause precision problems, resulting in too many decimal places. Therefore, you want to process the values accordingly in the template syntax. 1. Use<view>¥{{(money*0.03).toFixed(2)}} handling fee (rate 3%)</view> An error is reported and it cannot be used directly, nor can the methods in js be called. 2. SolutionSince functions in js cannot be called in {{}}, what should we do? WeChat has proposed a new concept, WXS (WeiXin Script), a scripting language for mini-programs that can call .wxs methods in WXML's {{}}. We create a new .wxs file // Support es4 syntax var filter = { numberToFixed: function(value){ return value.toFixed(2) } } // Export externally exposed properties module.exports = { numberToFixed: filter.numberToFixed } Import the file in .wxml: <!-- Import the .wxs file src as a relative path, module specifies the name of the current module--> <wxs module="filter" src="./numberToFixed.wxs"></wxs> Call the method in the .wxs module in {{}}: <view>¥{{filter.numberToFixed(money*0.03)}} handling fee (rate 3%)</view> SummarizeFrom Vue to Mini Programs, the biggest feeling when writing pages is that some processing or methods that Vue could previously implement with computed can now be called directly in {{}}, which makes it very convenient to process certain data. WeChat launched WXS to make up for the shortcoming that methods in js cannot be directly used in mini-programs {{}}. On the other hand, it also improves the performance of mini programs, each of which performs its own functions. This is the end of this article about how WeChat Mini Programs use functions directly in {{ }}. For more information about how WeChat Mini Programs use functions in {{ }}, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future! |
<<: Detailed analysis of matching rules when Nginx processes requests
>>: MySQL 5.7.18 Green Edition Download and Installation Tutorial
Use Code Cloud to build a Git code storage wareho...
Transaction A transaction is a basic unit of busi...
This article shares the specific code of JavaScri...
In web design, it is very important to use an org...
Import and export of Docker images This article i...
Table of contents 1. What kind of backup is a dat...
Image tag : <img> To insert an image into a ...
Overview In the previous chapter, we learned abou...
If Ubuntu is the most popular Linux operating sys...
Table of contents question: There are 2 tokens in...
Table of contents Causes of MySQL Table Fragmenta...
Several concepts Line box: A box that wraps an in...
Being an operation and maintenance engineer is a ...
This article example shares the specific code of ...
This article introduces the effect of website pro...