Home > Article > WeChat Applet > WeChat Mini Program Modular Detailed Introduction
This article mainly introduces relevant information about the detailed introduction of the modularization of WeChat mini programs. Friends who need it can refer to it
Modularization is to extract some common things and put them into a file. module.exports to expose interfaces. When we first created the project, there was a util.js file that was modularized.
/** * 处理具体业务逻辑 */ function formatTime(date) { //获取年月日 var year = date.getFullYear() var month = date.getMonth() + 1 var day = date.getDate() //获取时分秒 var hour = date.getHours() var minute = date.getMinutes() var second = date.getSeconds(); //格式化日期 return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':') } function formatNumber(n) { n = n.toString() return n[1] ? n : '0' + n } /** * 模块化导出暴露接口 */ module.exports = { formatTime: formatTime }
Usage method:
//导入模块化方式 var util = require('../../utils/util.js') Page({ data: { logs: [] }, onLoad: function () { this.setData({ logs: (wx.getStorageSync('logs') || []).map(function (log) { // 通过暴露的接口调用模块化方法 return util.formatTime(new Date(log)) }) }) } })
For more detailed introduction to the modularization of WeChat mini programs and related articles, please pay attention to the PHP Chinese website!