App Introduction
Introduction
App()
represents top-level applications and manages all pages and global data and provides lifecycle method. It is also a construction method, for generating the App
instance.
A Mini Program is an App
instance.
Typically, at the root directory of each Mini Program, there are three files.
- app.js: application logics
- app.acss: application style (optional)
- app.json: application configuration
Sample
Here is a simple app.json
.
copy
{
"pages": [
"pages/index/index",
"pages/logs/index"
],
"window": {
"defaultTitle": "Demo"
}
}
The configuration above indicates that two pages are included in the Mini Program, and the default title is Demo
for the application.
A simple app.js code is shown below. It has four life-cycle methods.
copy
App({
onLaunch(options) {
// called when opened
},
onShow(options) {
// called when opened or come foreground
},
onHide() {
// called when it goes background
},
onError(msg) {
// called on JavaScript error or API invoke exception
console.log(msg)
},
// global data
globalData: {
foo: true,
}
})