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:
{
"pages": [
"pages/index/index",
"pages/logs/logs"
],
"window": {
"defaultTitle": "Demo"
}
}
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:
App({
onLaunch(options) {
// Open for the first time.
},
onShow(options) {
// The Mini Program is started or re-opened from the background.
},
onHide() {
// The Mini Program is switched from the foreground to the background.
},
onError(msg) {
// A JavaScript error occurs or an API call fails in the Mini Program.
console.log(msg);
},
globalData: {
// The method is called to obtain global data.
name: 'mPaaS',
},
});