With the rapid development of WeChat Mini Programs, the jump page has become an important link in the development of Mini Programs. In applets, commonly used jump methods include navigateTo
, redirectTo
, switchTab
, and reLaunch
so on. This article will introduce the usage and characteristics of these jump methods in detail.
1. navigateTo
Method
navigateTo
It is one of the most commonly used jumping methods in applets. Its role is to open a new page and preserve the state of the current page. The specific usage is as follows:
wx.navigateTo({
url: 'pages/detail/detail?id=123',
success: (res) => {
console.log('跳转成功');
},
fail: (err) => {
console.error('跳转失败', err);
}
})
url
: The page path to jump to, which can take parameters.success
: The callback function for a successful jump.fail
: Callback function for jump failure.
It should be noted that navigateTo
there are restrictions on the jump levels of , and it can only jump to five levels of pages at most.
2. redirectTo
Method
redirectTo
Similar to navigateTo
, the difference is that redirectTo
the current page is closed and a new page is opened. The usage is as follows:
wx.redirectTo({
url: 'pages/login/login',
success: (res) => {
console.log('跳转成功');
},
fail: (err) => {
console.error('跳转失败', err);
}
})
After using redirectTo
to jump, the current page will be destroyed, and you cannot return to this page through the back button.
3. switchTab
Method
switchTab
It is used to jump to the TabBar page of the applet and close other non-TabBar pages. For example:
wx.switchTab({
url: 'pages/index/index',
success: (res) => {
console.log('跳转成功');
},
fail: (err) => {
console.error('跳转失败', err);
}
})
It should be noted that switchTab
it can only jump to the page with TabBar, and parameters are not allowed.
4. reLaunch
Method
reLaunch
It is a special jump method, it will close all pages and open a new page. It is often used to restart the applet or jump to the home page of the application. The usage is as follows:
wx.reLaunch({
url: 'pages/index/index',
success: (res) => {
console.log('跳转成功');
},
fail: (err) => {
console.error('跳转失败', err);
}
})
5. Other jumping methods
In addition to the several jump methods introduced above, the applet also provides some other jump methods, including navigateBack
, , navigateToMiniProgram
and so on. Select an appropriate jump mode based on actual needs.
Summarize
This article introduces the commonly used jumping methods in applets in detail, including navigateTo
, redirectTo
, switchTab
, reLaunch
and so on. Understanding the characteristics and usage of these jump methods can better perform the jump operation between Mini Program pages and improve user experience. During the development process, choose the appropriate redirection method according to specific needs, and pay attention to handling the redirection failure, so as to ensure that users can browse and use the applet normally.
I hope this article is helpful to you. If you have any questions or suggestions, please leave a message for discussion.