Get the App
SLTechnology News&Howtos  ›  Development  › 

Example Analysis of WeChat Mini Programs's Modularization and File scope

Shulou Source: shulou.com Published: 2022-06-01 05:46:47 09月18日 Update

Editor to share with you WeChat Mini Programs modularization and file scope of the example analysis, I believe that most people do not know much, so share this article for your reference, I hope you will learn a lot after reading this article. Let's learn about it!

File scope

Variables and functions declared in the JavaScript file are valid only in that file; variables and functions with the same name can be declared in different files without affecting each other.

The global application instance can be obtained through the global function getApp (). If you need global data, you can set it in App (), such as:

/ / app.jsApp ({globalData: 1}) / / a.js// The localValue can only be used in file a.js.var localValue ='a hammer / 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 = 'bunion / If a.js it run before b.js, now the globalData shoule be 2.console.log (getApp () .globalData) modularization

We can extract some common code into a separate js file as a module. The module can expose the interface only through module.exports or exports.

It is important to note that:

Exports is a reference to module.exports, so arbitrarily changing the direction of the exports in the module will cause unknown errors. So we recommend that developers use module.exports to expose module interfaces, unless you already know the relationship between the two.

Mini Program does not support the direct introduction of node_modules. When developers need to use node_modules, it is recommended to copy the relevant code to Mini Program's directory.

/ / common.jsfunction sayHello (name) {console.log ('Hello ${name}!')} function sayGoodbye (name) {console.log ('Goodbye ${name}!')} module.exports.sayHello = sayHelloexports.sayGoodbye = sayGoodbye

Use require (path) to bring in the common code in the files that need to use these modules.

Var common = require ('common.js') Page ({helloMINA: function () {common.sayHello (' MINA')} goodbyeMINA: function () {common.sayGoodbye know "Mina')}}) these are all the contents of this article entitled" sample Analysis of WeChat Mini Programs Modularization and File scope ". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: File module function small program code global function article example analysis content variable developer interface development different effective same not much only Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information MariaDB Huawei Shulou Technology Shulou Tech Info