We believe in the development of micro small procedures, will use a custom function function, then how do we write these JS in the applet, and how to call them in WXML in it? In other words direct call from the micro-channel defined in the applet in {} {}?
Micro-channel applet API: https://developers.weixin.qq.com/miniprogram/dev/reference/wxs/01wxs-module.html
First, create a common file JS
In the code of standardization and engineering projects, applet general public JS file created in the utils directory, so that we can call it in each module page. For example: a common file created here: time.wxs (PS: micro-channel applet must be a common file wxs suffix), as shown:
Second, the preparation of related application functions
Time.wxs open the file, and then write the function in which:
1 / * * 2 * Time Format: date is every minute . 3 * * @param {} timestamp: timestamp 13 . 4 * * @param {} type: the required time format conversion . 5 . CN * EG: 2020 Feb; EN: 2020.02.02 . 6 * / . 7 var timeFormat = { . 8 timestampToTime: function (timestamp, type) { . 9 IF (timestamp && type === 'CN' ) { 10 the let DATE = new new a Date (timestamp); // time stamp is required * 1000 10, 13, then the timestamp need by 1000 . 11 the let the Date.getFullYear the Y = () + 'on' ; 12 let M = (date.getMonth() + 1 < 10 ? (date.getMonth() + 1) : date.getMonth() + 1) + '月'; 13 let D = date.getDate() + '日'; 14 let h = date.getHours() < 10 ? '0' + (date.getHours()) + ':' : date.getHours() + ':'; 15 let m = date.getMinutes() < 10 ? '0' + (date.getMinutes()) : date.getMinutes(); 16 let s = date.getSeconds(); 17 18 return M + D + h + m; 19 } 20 if (timestamp && type === 'en') { 21 let date = newDATE (timestamp); // time stamp is required * 10 1000, the timestamp is not required by the case 13 1000 22 is the let the Date.getFullYear the Y = () + '.' ; 23 is the let M = (date.getMonth () . 1 + <10 '0' + (date.getMonth () +. 1):? '.' date.getMonth () +. 1) + ; 24 the let date.getDate D = () + '' ; 25 the let H = DATE .getHours () <10 '0' + (date.getHours ()) + ':': date.getHours () + ':'? ; 26 is the let m = date.getMinutes () <10 '0' +? ( date.getMinutes ()): date.getMinutes (); 27 the let S = date.getSeconds (); 28 29 return Y + M + D; 30 } 31 } 32 }; 33 is 34 is 35 // derived attribute of external exposure 36 module.exports = { 37 [ timestampToTime: timeFormat.timestampToTime 38 is }
Description:
1, define a global variable to begin var,
var 变量 = {};
2, writing functions. Function format:
函数名:function(参数) {},具体参见上面的例子
3、通过module.exports引用函数,写法如下:
module.exports = { 被使用的函数名:变量.具体函数 }
Third, how to try
1, the head of the page using the import documents, the introduction is:
1 <wxs module="time" src="../../utils/time.wxs"></wxs>
说明: module="文件名",我在utils文件夹里定义的文件名是time,所以这里的名称与之对应
2、函数调用
1 <view class="msg">{{time.timestampToTime(item.endTime, "cn")}}已开奖</view>