Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-04-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report