WeChat mini-Program (6) Modular detailed introduction, mini-program modular

Source: Internet
Author: User

Small Program (6) Modular detailed introduction, small program modular

Modularization refers to extracting some common things into a file and exposing interfaces through module. exports. When we first created a project, there was an util. js file that was modularized for processing time.

/*** Process specific business logic */function formatTime (date) {// obtain the year, month, and day var year = date. getFullYear () var month = date. getMonth () + 1 var day = date. getDate () // obtain the time, minute, and second var hour = date. getHours () var minute = date. getMinutes () var second = date. getSeconds (); // format the date 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}/*** modular export exposure interface */module. exports = {formatTime: formatTime}

Usage:

// Import the modular method var util = require ('.. /.. /utils/util. js') Page ({data: {logs: []}, onLoad: function () {this. setData ({logs: (wx. getStorageSync ('logs') | []). map (function (log) {// call the modular method return util through the exposed interface. formatTime (new Date (log ))})})}})

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.