Wechat applet page jump api summary and precautions

Hits: 0

When I wrote the WeChat applet for the first time, I encountered a page jump problem. I directly thanked the url address according to the previous habit, but it will prompt that the page to be jumped to is in the directory of the current page. , and then carefully looked at the document, summed up the relevant [api] of WeChat page jump , and some precautions

wx.navigateTo is used to keep the current page, jump to a page in the application, and use wx.navigateBack to return to the original page. For small programs with not many pages, it is usually recommended to use wx.navigateTo to jump to the original page to improve the loading speed. It is not recommended to use when there are too many pages.

wx.redirectTo When there are too many pages, the reserved pages will occupy the memory allocated to the applet by WeChat, or reach the 5-layer page stack limited by WeChat. At this time, we should consider choosing wx.redirectTo. wx.redirectTo() is used to close the current page and jump to a page in the application. Such a jump can avoid the page occupying the running memory before the jump, but the page needs to be reloaded when returning, which increases the display time of the returned page.

wx.reLaunch wx.reLaunch() is basically the same as wx.redirectTo(), except that wx.reLaunch() first closes all reserved pages in memory, and then jumps to the target page.

wx.switchTab For the page that jumps to the tab bar, it is better to choose wx.switchTab(), which will close all non-tab bar pages first. Secondly, you can also choose wx.reLaunch(), which can also jump from non-tab bar to tab bar, or jump between tab bars, the effect is the same as wx.switchTab(). Use other jump API to jump to the tab bar, the jump will fail.

wx.navigateBack is used to close the current page and return to the previous page or multi-level page. Developers can obtain the current page stack through getCurrentPages() and decide how many layers to return. The only parameter that this API needs to fill in is delta, which indicates the number of pages to be returned. If the value of delta is greater than the current number of pages that can be returned, return to the first page where the user entered the applet. When the value of delta is not filled in, it will be 1 by default (note that it is not 0 by default), that is, return to the previous page.

The above methods can make the page of WeChat applet jump, but there is a problem that needs to be paid attention to:
in the url of each jumped api, we can do it in two ways, the first is to use a relative path, for example :

wx.navigateTo({
      url: "../../pages/..."
    })

The second way is:

wx.navigateTo({
      url: "/pages/..."
    })

If we use these two ways, directly:

wx.navigateTo({
      url: "pages/...."
    })

It will treat this page as a page in the page subdirectory under the current page

Leave a Reply

Your email address will not be published.