Home > WeChat Applet > Mini Program Development > How to use wxs to calculate the obtained data in the applet

How to use wxs to calculate the obtained data in the applet

angryTom
Release: 2020-03-21 10:24:19
forward
3746 people have browsed it

This article introduces the data obtained by using wxs calculation in the applet. The written event function can be exposed through wxs code and called in wxml.

How to use wxs to calculate the obtained data in the applet

How the small program uses wxs to calculate the acquired data

A little bit of calculation is often required for the acquired data , for example, divide two numbers with only two decimal places.

wxs is often used for data processing. WXS code can be written in the tag in the wxml file, or in a file with the .wxs suffix. Case 1: Write in the wxml used a module definition that is only used in this file

step, 1 define this module, write the event processing function and expose it

var getEvery = function(totle,each) {   
 var eachPrice=(totle/each).toFixed(2);   
 return eachPrice; 
}  
module.exports.getEvery = getEvery;
Copy after login

(Recommended learning: Small program development)

This code is written in the wxml file. Each .wxs file and tag is a separate module. Each module has its own independent scope. That is, variables and functions defined in a module are private by default and are not visible to other modules. If a module wants to expose its internal private variables and functions to the outside world, it can only be achieved through module.exports.

step 2. Used in wxml: module name.Method name (parameter one, parameter two);

The parameters are in page.js The

{{m1.getEvery(goods.retailPriceJia,goods.childNum)}}
Copy after login

is enough.

Case 2: Defining wxs outside the reference file does not depend on the basic library version of the runtime and can run in all versions of small programs. So you don’t need to worry about the version step 1. The .wxs file is in the WeChat developer tools. You can directly create the .wxs file by right-clicking it and write the WXS script directly in it.

step 2 A single file definition can expose variable names and functions, one or more

var filters = { 
 toFix: function (value) {   
     return value.toFixed(2)//此处2为保留两位小数  
  }
}
var filt = {  
    toFix: function (value) {    
    return value.toFixed(1)
        //此处1为保留1位小数  
    }
}
module.exports = { 
 toFix: filters.toFix,  toFixs: filt.toFix
}
Copy after login

The .wxs file can be referenced by other .wxs files or tags in WXML .

step 3. Use

¥{{filt.toFixs(item.retailPriceJia)}}/个市场价:¥{{filt.toFixs(item.retailPrice)}}
Copy after login

on other pages. This is it. The effect is achieved by retaining two decimal places.

PHP Chinese website, a large number of free jquery video tutorials, welcome to learn online!

The above is the detailed content of How to use wxs to calculate the obtained data in the applet. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:zixun.jisuapp.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template