# Router
Start from base library version 2.16.1. Please remaining backward compatible.
Page router object.You can obtain a router object for the current page or custom component bythis.pageRouterorthis.router.
# The relative path of a route
Page routers haveswitchTab``reLaunch``redirectTo``navigateTo``navigateBack switchTab reLaunch redirectTo navigateTo navigateBack The function is the same;The only difference is that when a method is called in the page pathfinder, the relative path is always relative to the page or custom component thatthisrefers to.
For example, for the following example code:
// index/index.js
Page({
wxNavAction: function () {
wx.navigateTo({
url: './new-page'
})
},
routerNavAction: function () {
this.pageRouter.navigateTo({
url: './new-page'
})
}
})
The js code for the pageindex / indexis shown above.If you have jumped to a new pagepack / index,Before calling thewxNavActionmethod above, the new page path for the jump will bepack/new-page;If therouterNavActionmethod is called, the new page path of the jump is stillindex/new-page。
In other words,this.pageRouterhas better base path stability.In general, it is preferable to usethis.pageRouter.navigateToinstead ofwx.navigateTo [()].
# Routing relative to custom component paths
This.pageRouterandthis.routerwill get the same page router object in the page.
But if called in a custom component,this.pageRouterredirects the page where the custom component is located, whilethis.routeris the path relative to the custom component itself.