WeChat applet example of using functions directly in {{ }}

WeChat applet example of using functions directly in {{ }}

Preface

In 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. Solution

Since 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>

Summarize

From 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

Recommend

Is it easy to encapsulate a pop-up component using Vue3?

Table of contents Summary put first: 🌲🌲 Preface: ...

Analysis and application of irregular picture waterfall flow principle

The layout problem of irregular picture walls enc...

Detailed analysis of binlog_format mode and configuration in MySQL

There are three main ways of MySQL replication: S...

Teach you how to implement Vue3 Reactivity

Table of contents Preface start A little thought ...

MySQL sorting Chinese details and examples

Detailed explanation of MySQL sorting Chinese cha...

IE8 Beta 1 has two areas that require your attention

<br />Related articles: Web skills: Multiple...

Tips for viewing text in Linux (super practical!)

Preface In daily development, we often need to pe...

Web front-end development course What are the web front-end development tools

With the development of Internet technology, user...

MYSQL METADATA LOCK (MDL LOCK) MDL lock problem analysis

1. Introduction MDL lock in MYSQL has always been...

Detailed explanation of the definition and function of delimiter in MySQL

When you first learn MySQL, you may not understan...

A brief discussion on several specifications of JS front-end modularization

Table of contents Preface The value of front-end ...

64-bit CentOs7 source code installation mysql-5.6.35 process sharing

First install the dependent packages to avoid pro...

Detailed tutorial on deploying Springboot or Nginx using Kubernetes

1 Introduction After "Maven deploys Springbo...

jQuery implements sliding tab

This article example shares the specific code of ...