JavaScript
engine to provide a runtime environment for the developer's JavaScript code, as well as the unique features of the TMF mini program.JavaScript
file, which runs at the launch of the mini program and continues until the mini program is terminated. This behavior is similar to ServiceWorker, hence the logical layer is also referred to as the App Service.JavaScript
, we have incorporated additional features to facilitate the development of mini programs:JavaScript
capabilities available in the web, such as window
and document
, cannot be used.App()
function is used to register a mini-program. It accepts an Object
parameter, which designates the lifecycle callbacks of the mini-program, among other things.app.js
. It must be called only once, otherwise unforeseen consequences may occur.Attribute | Type | Description | Trigger Timing |
function | Lifecycle Callback - Monitor Mini Program Initialization Page Unload | Upon completion of the mini program initialization (triggered only once globally) | |
function | Lifecycle Callback - Monitor the Initial Display of Mini Program | Upon the launch of the mini program, or when it transitions from the backend to the frontend display. | |
function | Lifecycle Callback - Monitor the Initial Concealment of Mini Programs | When the mini program transitions from the frontend to the backend. | |
function | Error listener function | Triggered when the mini program encounters script error, or when an API call fails, accompanied by error information. | |
function | Page Non-existence Listener Function | The function is triggered and called back with page information when the page the mini program intends to open does not exist. | |
Other | any | Developers can add any function or data to the Object parameter, which can be accessed using this . | - |
App({onLaunch(options) {// Do something initial when launch.},onShow(options) {// Do something when show.},onHide() {// Do something when hide.},onError(msg) {console.log(msg)},globalData: 'I am global data'})
App({onPageNotFound(res) {wx.redirectTo({url: 'pages/...'}) // If it's a tabbar page, use wx.switchTab.}})
getApp()
function can be used to obtain the mini program's App
instance.Field | Type | Description | Earliest Version |
allowDefault | boolean | Returns the default implementation when the App is undefined. When the App is invoked, the attributes defined in the default implementation will be overridden and merged into the App. | - |
// other.js
const appInstance = getApp()
console.log(appInstance.globalData) // I am global data
getApp()
within functions defined in App()
. The app instance can be accessed using this
.App
's onLaunch
and onShow
, or through getLaunchOptionsSync.Scene Value | Scene | Significance of appId |
1020 | List of related mini programs on the We Chat Official Account profile page. | Originating from We Chat Official Account |
1035 | Custom menu of We Chat Official Account | Originating from We Chat Official Account |
1036 | App message sharing card | Originating from App |
1037 | Mini program opening another mini program | Originating from mini program |
1038 | Returning from another mini program | Originating from mini program |
1043 | We Chat Official Account template message. | Originating from We Chat Official Account |
Routing method | Page stack behavior |
Initialization | New page enters the stack |
Opening the page | New page enters the stack |
Page redirection | Current page enters the stack, new page enters the stack |
Page return | Pages continuously pop from the stack until the target return page is reached. |
Tab switching | All pages pop from the stack, leaving only the new Tab page. |
Reload | All pages pop from the stack, leaving only the new page. |
getCurrentPages()
function is used to obtain instances of the current page stack, presented in array format in accordance with the stack order. The first element represents the home page, while the last element signifies the current page.getCurrentPages()
during the App.onLaunch
phase, as the page
has not yet been generated at this point.Routing method | Trigger Timing | Pre-routing page | Post-routing page |
Initialization | The first page opened by the mini program. | - | onLoad, onShow |
Opening the page | Invoke the API wx.navigateTo or use the component <navigator open-type="navigateTo"/>. | onHide | onLoad, onShow |
Page redirection | Invoke the API wx.redirectTo or use the component <navigator open-type="redirectTo"/>. | onUnload | onLoad, onShow |
Page return | Invoke the API wx.navigateBack, use the component <navigator open-type="navigateBack">, or the user presses the return button in the top left corner. | onUnload | onShow |
Tab switching | Invoke the API wx.switchTab, utilize the component <navigator open-type="switchTab"/>, or the user switches the Tab. | - | For various scenarios, see the table below. |
Restart | Invoke the API wx.reLaunch or use the component <navigator open-type="reLaunch"/>. | onUnload | onLoad, onShow |
The current page | Post-routing page | Triggered lifecycle (in order) |
A | A | Nothing happend |
A | B | A.onHide(), B.onLoad(), B.onShow() |
A | B (Reopened) | A.onHide(), B.onShow() |
C | A | C.onUnload(), A.onShow() |
C | B | C.onUnload(), B.onLoad(), B.onShow() |
D | B | D.onUnload(), C.onUnload(), B.onLoad(), B.onShow() |
D (Entered via forwarding) | A | D.onUnload(), A.onLoad(), A.onShow() |
D (Entered via forwarding) | B | D.onUnload(), B.onLoad(), B.onShow() |
// app.jsApp({globalData: 1})
// a.js// The localValue can only be used in file a.js.const localValue = 'a'// Get the app instance.const app = getApp()// Get the global data and change it.app.globalData++
// b.js// You can redefine localValue in file b.js, without interference with the localValue in a.js.const localValue = 'b'// If a.js it run before b.js, now the globalData shoule be 2.console.log(getApp().globalData)
// common.jsfunction sayHello(name) {console.log(Hello ${name} !
)}function sayGoodbye(name) {console.log(Goodbye ${name} !
)}module.exports.sayHello = sayHelloexports.sayGoodbye = sayGoodbye
const common = require('common.js')Page({helloMINA() {common.sayHello('MINA')},goodbyeMINA() {common.sayGoodbye('MINA')}})
require
currently does not support absolute paths. wx.onCompassChange(function (res) {console.log(res.direction)})
Sync
are synchronous APIs, such as wx.setStorageSync, wx.getSystemInfoSync, etc. In addition, there are some other synchronous APIs. For more details, see relevant descriptions in the API documentation.try {wx.setStorageSync('key', 'value')} catch (e) {console.error(e)}
Field | Type | Required | Description |
success | function | No | Callback Function of Successful Interface Call |
fail | function | No | Callback Function of Successful Interface Call |
complete | function | No | Callback function executed upon the completion of the interface invocation (both successful and unsuccessful invocations) |
Other | any | - | Other parameters defined by the interface |
success
, fail
, and complete
functions will receive an Object
type parameter, containing the following fields:Attribute | Type | Description |
errMsg | string | Error information, which returns ${apiName}:ok if the invocation is successful |
errCode | number | Error code, supported only by certain APIs. For specific meanings, see the corresponding API documentation. The code is 0 in case of success. |
Other | any | Other data returned by the interface |
wx.login({success(res) {console.log(res.code)}})
Was this page helpful?