WeChat Open Platform

Mini Programs Documentation

Page routes

The routes for all pages in a Mini Program are managed by the framework.

Page stacks

The framework maintains all current pages in stack form. When route switching occurs, the page stack will behave as follows:

Route method Page stack behavior
Initialization New page is stacked
Open new page New page is stacked
Page redirect Current page popped from stack, new page is stacked
Page back Pages are continuously popped from stack until the target page is returned to, new pages are stacked
Tab switch All pages are popped from stack with just a new tab page left behind
Reload All pages are popped from stack with just a new page left behind

getCurrentPages()

The getCurrentPages() function is used to get an instance of the current page stack, which is given in array form in stack order. The first element is the homepage and the last element is the current page.

Tip: Do not try to modify the page stack, this will cause route and page status errors.

Route methods

The route trigger methods and page lifecycle functions are as follows:

Route method When triggered First page of route Last page of route
Initialization The first page of the Mini Program is opened onLoad, onShow
Open new page API wx.navigateTo is called or component <navigator open-type="navigateTo"/> is used onHide onLoad, onShow
Page redirect API wx.redirectTo is called or component <navigator open-type="redirectTo"/> is used onUnload onLoad, onShow
Page back API wx.navigateBack is called or component <navigator open-type="navigateBack"> is used, or user presses Back button in top left corner onUnload onShow
Tab switch API wx.switchTab is called or component <navigator open-type="switchTab"/> is used, or user switches Tab Please refer to the following table for each situation
Restart API wx.reLaunch is called or component <navigator open-type="reLaunch"/> is used onUnload onLoad, onShow

Lifecycles corresponding to Tab switch (in the examples, pages A and B are the tab bar pages, C is a page opened from Page A, and page D is a page opened from page C):

Current page Last page of route Trigger lifecycle (in order)
A A Nothing happened
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 from sharing) A D.onUnload(), A.onLoad(), A.onShow()
D (entered from sharing) B D.onUnload(), B.onLoad(), B.onShow()

Tips:

  • navigateTo and redirectTo can only open non-tab bar pages.
  • switchTab can only open tab bar pages.
  • reLaunch can open any page.
  • The tab bar at the bottom of the page is determined by the page, that is, as long as a page is defined as a tab bar page, there will always be a tab bar at the bottom.
  • The parameters brought when calling a page route can be obtained in onLoad on the target page.