All Products
Search
Document Center

Introducing global configuration for Mini Program

Last Updated: Feb 04, 2021

App() is used to obtain the top-layer application that manages all the pages, global data, and lifecycle callbacks. App() is also a constructor that can be used to generate an app instance.

A Mini Program is an app instance. Generally, the top layer of each Mini Program consists of three files.

  • app.json: contains the application configurations.
  • app.js: contains the application logic.
  • app.acss: contains the application style. This file is optional.

Sample code

  • The following code snippet shows a simple app.json file:
  1. {
  2. "pages": [
  3. "pages/index/index",
  4. "pages/logs/logs"
  5. ],
  6. "window": {
  7. "defaultTitle": "Demo"
  8. }
  9. }
  1. As shown in the code, the Mini Program contains two pages: "index" and "logs". The default title of the window is "Demo".
  • The following code snippet shows a simple app.js file:
  1. App({
  2. onLaunch(options) {
  3. // Open for the first time.
  4. },
  5. onShow(options) {
  6. // The Mini Program is started or re-opened from the background.
  7. },
  8. onHide() {
  9. // The Mini Program is switched from the foreground to the background.
  10. },
  11. onError(msg) {
  12. // A JavaScript error occurs or an API call fails in the Mini Program.
  13. console.log(msg);
  14. },
  15. globalData: {
  16. // The method is called to obtain global data.
  17. name: 'mPaaS',
  18. },
  19. });