App represents the top application, which administrates all page and global data, and provides a life cycle method. It is also a construction method generating App instance. A mini program is an App instance.

Introduction

The top of each mini program generally contains three files:

  • app.acss:app style (optional)
  • app.js:app logic
  • app.json:App configuration

Here is a simple app.json:

  1. {
  2. "pages": [
  3. "pages/index/index",
  4. "pages/logs/index"
  5. ],
  6. "window": {
  7. "defaultTitle": "Demo"
  8. }
  9. }

In the above configuration, it is specified that the mini program contains two pages with the default title of “Demo” in the window.

“App” provides four events to set the hook method:

  • onLaunch:mini program Start
  • onShow:mini program switch to foreground
  • onHide:mini program switch to background
  • onError: mini program errors

    Here’s a “simple app.js” code as follows:

    1. App({
    2. onLaunch(options) {
    3. // 小程序初始化
    4. },
    5. onShow(options) {
    6. // 小程序显示
    7. },
    8. onHide() {
    9. // 小程序隐藏
    10. },
    11. onError(msg) {
    12. console.log(msg)
    13. },
    14. globalData: {
    15. foo: true,
    16. }
    17. })

    App()

App() accepts an “object” as a parameter to configure the life cycle of the mini program and others.

Description of parameters:

Attribute Type Description Triggering time
onLaunch Function Monitor initialization It is globally triggered when the initialization is completed.
onShow Function Monitor display When mini program starts or is entered from background to foreground
onHide Function Monitor hiding When it’s entered from foreground to background
onError Function Monitor errors When “js” errors occur

Definition of foreground and background: When users clickthe upper left for closing or press Home key to leave the “mPaaS” client, the mini program will not be uninstalled directly, but will run in the background. When you enter the “mPaaS” client again or open the mini program again, you will enter the foreground from the background.

Only when the mini program enters the background for certain time, or takes up too many system resources, will it be truly uninstalled.

Parameters of onLaunch/onShow method

Attribute Type Description
query Object Query of current mini program
path String The page address of the current
Mini program

The method of parameter passing for “Native” starting:

  1. //启动小程序demo
  2. Bundle param = new Bundle();
  3. String queryParam = "param1=value1&param2=value2&param3=value3";
  4. param.putString("query",queryParam);
  5. LauncherApplicationAgent.getInstance().getMicroApplicationContext().startApp(s:null,s1:"2018080616290001",param);

Here’s the parameter transmission method for URL starting: “query” is parsed from the “query” field of the start parameter, and “path” is parsed from the “page” field of the starting parameter. Here are examples in the following URL:

  1. alipays://platformapi/startapp?appId=1999&query=number%3D1&page=x%2Fy%2Fz
  • The “query” parameter is parsed as follows:
    1. number%3D1 === encodeURIComponent(‘number=1’)
  • The “path” parameter is parsed as follows:
    1. x%2Fy%2Fz === encodeURIComponent(‘x/y/z’)

The page will switch to the homepage by default when the page cannot be accessed.

Then, when usersstart the mini program for the first time, this parameter can be obtained by the onLaunch method. Or it can be also obtained by the onShow method when the mini program in the background is re-opened with the schema.

  1. App({
  2. onLaunch(options) {
  3. // 第一次打开
  4. // options.query == {number:1}
  5. },
  6. onShow(options) {
  7. // 从后台被 scheme 重新打开
  8. // options.query == {number:1}
  9. },
  10. })

getApp()

We provide a global “getApp()” function, which can obtain instances of mini programsand be generally used to obtain top applications among various sub-sides.

  1. var app = getApp()
  2. console.log(app.globalData) // 获取 globalData

Note:

  • “App()” must be called in “app.js” and cannot be called for multiple times.
  • Don’t call “getApp()” in the functions defined in “App()”, use “this” to get the “app” instance.
  • Don’t call “getCurrentPages()” in “onLaunch” because“page” has not yet been created.
  • After getting the instance through “getApp()”, do not call the life cycle function.

Global data can be set in “App()”, and each sub-page can obtain global application instances through the global function “getApp()”. E.g:

  1. // app.js
  2. App({
  3. globalData: 1
  4. })
  1. // a.js
  2. // localValue 只在 a.js 有效
  3. var localValue = 'a'
  4. // 生成 app 实例
  5. var app = getApp()
  6. // 拿到全局数据,并改变它
  7. app.globalData++
  1. // b.js
  2. // localValue 只在 b.js 有效
  3. var localValue = 'b'
  4. // 如果 a.js 先运行,globalData 会返回 2
  5. console.log(getApp().globalData)

In the above code, both “a.js” and “b.js” declare the variable “localValue”, they will not affect each other because the variables and functions declared by each script are only valid in the file.

app.json

“app.json” is used for global configuration, determining the path of the page file, window performance, and setting the network timeout and multiple tabs, etc. The following is a simple configuration of“app.json” that includes some configuration options.

  1. {
  2. "pages": [
  3. "pages/index/index",
  4. "pages/logs/index"
  5. ],
  6. "window": {
  7. "defaultTitle": "Demo"
  8. }
  9. }

app.json configuration options are as follows:

File Type Required Description
pages String Array Yes Set page path
window Object No Set the window performance of the default page
tabBar Object No Set the performance of the bottom “tab”

pages

The “pages” attribute is an array with each item as a string, which is used to specify the mini program. Each item represents the path information of the corresponding page, and the first item of the array represents homepage of the mini program. Adding/reducing pages in the mini program requires modification to the “pages” array.

The page path does not need to write the “js” suffix. The framework will automatically load the “.json”, “.js”, “.axml”, and “.acss” files with the same name. For example, if the development directory is this:

  1. .
  2. ├── app.acss
  3. ├── app.js
  4. ├── app.json
  5. └── pages
  6. ├── index
  7. ├── index.axml
  8. ├── index.js
  9. └── index.json
  10. └── logs
  11. ├── logs.axml
  12. └── logs.js

app.json should be written as follows.

  1. {
  2. "pages": [
  3. "pages/index/index",
  4. "pages/logs/logs"
  5. ]
  6. }

window

The window attribute is used to set the status bar, navigation bar, title, and backgroundcolor of window for the general purpose of mini programs.

The sub attributes include “titleBarColor”, “defaultTitle”, “pullRefresh”, “allowsBounceVertical”.

Flie Type Required Description
titleBarColor 十进制 No Background color of navigation bar
defaultTitle String No Page title
pullRefresh Boolean No Whether to allow pull-down refresh. It is false by default
allowsBounceVertical String(YES/NO) No Whether the page supports vertical dragging is beyond the actual content. It is yes by default

Example:

  1. {
  2. "window": {
  3. "defaultTitle": "支付宝接口功能演示"
  4. }
  5. }

tabBar

If your mini program is a multi-tab app (Page can be switched by the bottom bar of the client window), then you can specify the performance of the tab bar through the “tabBar” configuration item, and the corresponding page displayed when the tab is switched.

Note:

  • The page switched by page jump (my.navigateTo) or page redirection (my.redirectTo) will not display the tab bar at the bottom even if it is a page defined in the tabBar configuration.
  • The first page of the tabBar must be the homepage.

tabBar configuration

File Type Required Description
textColor HexColor No Text color
selectedColor HexColor No select text color
backgroundColor HexColor No Background color
items Array Yes each tab configuration

Each item configuration

File Type Required Description
pagePath String Yes Set page path
name String Yes Name
icon String No Usual icon path
activeIcon String No Bright icon path

The recommended size of the icon is 60*60px, and the system will stretch/zoom out any images that are uploaded. E.g:

  1. {
  2. "tabBar": {
  3. "textColor": "#dddddd",
  4. "selectedColor": "#49a9ee",
  5. "backgroundColor": "#ffffff",
  6. "items": [
  7. {
  8. "pagePath": "pages/index/index",
  9. "name": "首页"
  10. },
  11. {
  12. "pagePath": "pages/logs/logs",
  13. "name": "日志"
  14. }
  15. ]
  16. }
  17. }

Startup parameters

You can bring the “page” and “query” parameters when opening a mini program from “native” code. “Page” is used to specify the path to open a specific page, and “query” is used to take parameters.

iOS code example

  1. NSDictionary *param = @{@"page":@"pages/card/index", @"query":@"own=1&sign=1&code=2452473"};
  2. MPNebulaAdapterInterface startTinyAppWithId:@"1234567891234568" params:param];

Android code example

  1. Bundle param = new Bundle();
  2. param.putString("page", "pages/card/index");
  3. param.putString("query", "own=1&sign=1&code=2452473");
  4. MPNebula.startApp("1234567891234568",param)