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
Using win docker-desktop, I want to connect to co...
Table of contents 1. Pull the Redis image 2. Crea...
1. Create a repository in the specified directory...
1 Background Recently, some performance issues ha...
GitHub address, you can star it if you like it Pl...
Table of contents 1.Linux login interface 2. Writ...
Implementation requirements The form imitating El...
1.1 Introduction By enabling the slow query log, ...
Types of Indexes in MySQL Generally, they can be ...
As shown in the figure: Check port usage: sudo ne...
Linux online installation related commands: yum i...
This article describes how to export and import ....
In addition to B-Tree indexes, MySQL also provide...
1. MYSQL index Index: A data structure that helps...
Table of contents Bidirectional binding principle...