May 18, 2021 WeChat Mini Program Development Document
App()
function is used to register a small program.
Accepts an object parameter that specifies the life cycle function of a small program, and so on.
Object parameter description:
Property | Type | Describe | Trigger timing |
---|---|---|---|
onLaunch | Function | Lifecycle function - Listen for the initialization of small programs | When the program initialization is complete, onLaunch is triggered (globally only once) |
onShow | Function | Life cycle function - listen for small program display | OnShow is triggered when the small program starts, or when it enters the foretime display from the background |
onHide | Function | Lifecycle function - Listen for small programs to hide | OnHide is triggered when a small program enters the background from the front desk |
onError | Function | Error listening function | OnError is triggered with an error message when a script error occurs in a small program, or when an api call fails |
onPageNotFound
|
Function
|
The page does not have a listening function
|
When a small program appears that the page you want to open does not exist, the function is called back with page information, as detailed below
|
Other | Any |
|
Developers can add any function or data to the Object parameter, which can
this
with this
|
Fore desk, background definition: When the user clicks on the upper left corner to close, or press the device Home key to leave WeChat, the small program is not directly destroyed, but into the background; It is important to note that only when a small program enters the background for a certain amount of time, or if the system resources are too high, will it be truly destroyed.
Close the applet (the base library version 1.1.0 start support): When the user is scanned, forwarding the entrance ( Scene It is 1007, 1008, 1011, 1025) to enter the applet, and if you don't have a small procedure, the applet will be destroyed. The small program running mechanism changes in the base library version 1.4.0 : The last closing logic is not applicable in the new version, Detail
Sample code:
App({
onLaunch: function(options) {
// Do something initial when launch.
},
onShow: function(options) {
// Do something when show.
},
onHide: function() {
// Do something when hide.
},
onError: function(msg) {
console.log(msg)
},
globalData: 'I am global data'
})
Field | Type | Description |
---|---|---|
path | String | Open the path to the small program |
query | Object | Open the qury of the little program |
scene | Number | Open the scene value of the small program |
shareTicket | String | ShareTicket, see for more forwarding information |
referrerInfo | Object | Returns this field when the scene is opened by another small program |
referrerInfo.appId | String | The appId of the source applet |
referrerInfo.extraData | Object | The data from the source program |
Scene values See in detail.
The following scenarios support returning to the referrerInfo.appId:
The scene value | Scene | What the appId information means |
---|---|---|
1020 | A list of related gadgets on the public number profile page | Return to the source public number appId |
1035 | Public number custom menu | Return to the source public number appId |
1036 | The app shares the message card | Return to the source app AppId |
1037 | A small program opens a small program | Returns the source applet appId |
1038 | Return from another small program | Returns the source applet appId |
1043 | Public number template message | Return to the source public number appId |
Base library 1.9.90 starts to support, and low versions need to be compatible
When the page you want to open does not exist, the listener is recalled with the following information:
Field | Type | Description |
---|---|---|
path | String | There is no path to the page |
query | Object | Open a query that does not exist for the page |
isEntryPage | Boolean | Whether the first page of this launch (e.g. from the entrance to sharing, etc., the first page is a developer-configured sharing page) |
Developers can redirect processing in the onPageNotFound callback, but must synchronize processing in the callback, and asynchronous processing, such as setTimeout asynchronous execution, is not valid.
Example code:
App({
onPageNotFound(res) {
wx.redirectTo({
url: 'pages/...'
})
}
})
Attention:
We provide a global
getApp()
that can get instances of small programs.
// other.js
var appInstance = getApp()
console.log(appInstance.globalData) // I am global data
Attention:
App()
be registered .js the app's website and cannot be registered more than one.
Instead of calling
App()
in functions
getApp()
use
this
get the app instance.
Do not call
getCurrentPage()
generated.
getApp()
functions privately after getting an instance through getApp().