Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use global variables in WeChat Mini Programs

Shulou Source: shulou.com Published: 2022-06-01 04:10:32 09月22日 Update

This article mainly introduces how to use global variables in WeChat Mini Programs. It is very detailed and has a certain reference value. Friends who are interested must finish reading it.

Use of global variables

Each Mini Program needs to call the App method in app.js to register Mini Program examples, bind lifecycle callback functions, error listeners, and the page does not exist listening functions.

For more information on the meaning and use of the parameters, please refer to the App reference documentation.

The whole Mini Program has only one instance of App, which is shared by all pages. Developers can use the getApp method to obtain globally unique App examples, obtain data on App, or call functions that developers register on App.

When we do Mini Program, we often need a large number of requests, and the requested domain names are all the same. We can store the domain name in the global variable, which will facilitate later request for domain name modification. (frequently used items such as user_id, unionid, user_info, etc., can be placed in global variables)

/ / app.jsApp ({globalData: {user_id: null, unionid:null, url: "https://xxx.com/index.php/Home/Mobile/", / / requested domain name user_info:null}})

Remember to reference app.js when using it on the page. Mini Program already provides a method.

/ / index.js// get application instance const app = getApp () / / get app//let url = app.globalData.url / / to use the method, you can first define or directly use app.globalData.urlwx.request ({url: app.globalData.url + 'checkfirst', / /) to call method:'POST', header: {"Content-Type": "application/x-www-form/"} data: {}, success: (res) = > {} above are all the contents of the article "how to use global variables in WeChat Mini Programs" Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Tags: Programs globals variables domain names methods functions pages references content examples developers examples articles development monitoring same value usage interests parameters Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn MySQL Apple NVidia Microsoft