Modularization of WeChat mini-app tutorials and Wechat mini-app Modularization

Source: Internet
Author: User

Modularization of mini-program tutorials and mini-program modularization

Series of articles:

Modularization of mini-program tutorials

Registration page for applet tutorials

Registration Procedures for mini-program tutorials

File Scope

Variables and functions declared in JavaScript files are only valid in this file. Variables and functions with the same name can be declared in different files without affecting each other.
You can use the global function getApp () to obtain global application instances. If you need global data, you can set it in App (), for example:

// app.jsApp({ globalData: 1})
// a.js// The localValue can only be used in file a.js.var localValue = 'a'// Get the app instance.var app = getApp()// Get the global data and change it.app.globalData++
// b.js// You can redefine localValue in file b.js, without interference with the localValue in a.js.var localValue = 'b'// If a.js it run before b.js, now the globalData shoule be 2.console.log(getApp().globalData)

Modular

We can extract some public code into a separate js file as a module. The interface can be exposed only through module. exports.

// common.jsfunction sayHello(name) { console.log('Hello ' + name + '!')}module.exports = { sayHello: sayHello}

In files that need to use these modules, use require (path) to introduce public code.

var common = require('common.js')Page({ helloMINA: function() { common.sayHello('MINA') }})

Thank you for reading this article. I hope it will help you. Thank you for your support for this site!

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.