一、路由跳转的几种方式
1.wx.navigateTo()
- 保留当前页面,跳转到应用内的某个页面。但是不能跳到 tabbar 页面
wx.navigateTo({
url: '/pages/detail/detail?id=1'
})
2.wx.redirectTo():
- 关闭当前页面,跳转到应用内的某个页面。但是不允许跳转到 tabbar 页面
wx.redirectTo({
url: `/pages/detail/detail`,
})
3.wx.switchTab():
- 跳转到 tabBar 页面,并关闭其他所有非 tabBar 页面(不可以携带参数)
wx.switchTab({
url: `/pages/detail/detail`,
})
4.wx.navigateBack()
- 关闭当前页面,返回上一页面或多级页面。可通过 getCurrentPages() 获取当前的页面栈,决定需要返回几层
wx.navigateBack({
delta:1
})
5.wx.reLaunch():
wx.reLaunch({
url: '/pages/detail/detail'
})
二、跳转路由传递参数
- 下面代码中就是navigator目录下的navigator页面,title是参数.
- navigator下redirect属性是值在当前页打开.如果不加redirect就是跳转到新页面.都可以携带参数.
<view class="conent">
<navigator url="../navigator/navigator?title=我是navigate" >跳转到新页面</navigator>
<navigator url="../redirect/redirect?title=我是redirect" redirect>在当前页打开</navigator>
</view>
<view style="text-align:center"> {{title}} </view>
- 在navigatort.js中通过options可以获取到title,代码如下
Page({
onLoad: function(options) {
this.setData({
title: options.title
})
}
<view style="text-align:center"> {{title}} </view>
- 在redirect.js中通过options可以获取到title,代码如下
Page({
onLoad: function(options) {
this.setData({
title: options.title
})
}
})