1. Application life cycle
The process of applet from start->run->destroy
Application lifecycle functions
Application lifecycle functions are declared in app.js, and the execution sequence is as follows
- onLaunch (triggered when the applet is initialized, only triggered once globally, and executed before all page cycle functions)
- onShow (triggered when the applet starts, or enters the foreground display from the background, and executes before all page cycle functions)
- onHide (triggered when the applet enters the background display from the foreground)
- onError (triggered when a script error occurs in the applet or an error is reported in an API call)
- onPageNotFound (triggered when the page to be opened by the applet does not exist)
- onUnhandledRejection (triggered when the applet has an unhandled Promise rejection)
- onThemeChange (triggered when the system switches the theme)
2. Page life cycle
Each page load->render->destroy process
Page lifecycle functions
The page life cycle function is declared in the .js file of the page, and the execution sequence is as follows
-
onLoad (executed when the page is created, only called once in a page life cycle, can receive an object parameter, which contains the
wx.navigateTo()
parameters spliced in the url when the page is opened, often used for initialization and dumping data)①It is executed when the page displayed when the applet is opened
② It is executed when
wx.navigateTo
the opened③It is called and executed when the page is opened
wx.switchTab
for the first time -
onShow (executed when the page appears in the foreground)
-
onReady (executed when the page is rendered for the first time, monitor the completion of the first rendering of the page, and only call once in a page life cycle, you can modify the page content in this function, such as calling wx.setNavigationBarTitle)
①It is executed when the page displayed when the applet is opened
② It is executed when
wx.navigateTo
the opened③It is called and executed when the page is opened
wx.switchTab
for the first time -
onHide (the page is not closed, it is only executed when it changes from the foreground to the background)
① Normal page use Execute when
wx.navigateTo
switching to other pages②The tabBar page is used to execute when
wx.switchTab
switching to other pages -
onUnload (executed when the page is destroyed, only called once in a page life cycle)
-
onPullDownRefresh (executed when pull-down refresh is triggered)
-
onReachBottom (executed when the page bottoms out)
-
onShareAppMessage (executed when the page is shared by the user)
-
onPageScroll (executed when the page scrolls)
-
onResize (executed when the page size changes)
-
onTabItemTap (when the current tabBar page is triggered when the tab is clicked, if a custom tabbar is used, it will not be triggered. An object parameter can be received, which contains the index, pagePath, text and other data of the clicked tab)
Note:
① wx.switchTab
When calling (only jump to the tabBar page ), close all other non-tabBar pages , that is, execute all non-tabBar unclosed pagesonUnload
② wx.reLaunch
When , all unclosed pages will be closed (including the current page), that is, all unclosed pages will executeonUnload
③ wx.redirectTo
When (can only jump to non-tabBar pages ), the current page is closed and the current page is executedonUnload
④ wx.navigateTo
When , save the current page (use wx.navigateBack
to return), and jump to a page in the application. But you can't jump to the tabbar page, the current page executesonHide
⑤ When calling wx.navigateBack
(returning to the previous page or multi-level page), all pop-up pages between the current page (including the current page) and the target page are closed , and the executiononUnload