页面导航的两种方式
-
声明式导航
在页面上声明 <navigator>导航组件,url属性是导航地址
-
编程式导航
调用小程序的导航API,实现页面跳转
声明式导航
导航到tabBar页面
tabBar页面是被配置为tabBar的页面
navigator组件,需要指定url属性和open-type属性
url: 跳转页面的地址 必须以 / 开头
open-type 跳转的方式 值为switchTab
<navigator url="/pages/home/home" open-type="switchTab">跳转到home</navigator>
导航到非tabBar页面
tabBar页面是被配置为tabBar的页面
navigator组件,需要指定url属性和open-type属性
url: 跳转页面的地址 必须以 / 开头
open-type 跳转的方式 值为navigate
<navigator url="/pages/cart/cart" open-type="navigate">cart</navigator>
导航到非tabBar页面时,open-type="navigate" 可以省略
后退导航
如果要后退到上一页面或多级页面,需要指定open-type和delta属性
open-type 的值必须是navigateBack ,表示要进行后退导航
delta 的值必须是数字,表示后退的层级
<navigator open-type="navigateBack" delta="1">返回</navigator>
如果只是返回到上一页面,可以省略delta=“1”,因为默认值为1
传参
/pages/cart/cart?id=2&name=zs
-
参数和路径直接使用?分割
-
参数名和参数值之间用 =
-
不同参数用 & 分割
<navigator url="/pages/cart/cart?id=2&name=zs" open-type="navigate">跳转到home</navigator> <navigator url="/pages/cart/cart?id={{num}}&name=zs" open-type="navigate">跳转到home</navigator>
编程式导航
导航到tabBar
wx.switchTab({ url: '/pages/home/home', })
导航到非tabBar
wx.navigateTo({ url: '/pages/cart/cart', })
后退导航
wx.navigateBack({ delta:1 })
传参
wx.switchTab({ url: '/pages/cart/cart?id=23&name=zs', }) wx.switchTab({ url: `/pages/cart/cart?id=${this.data.num}&name=zs`, })
在onLoad中接收参数
onLoad(options) { console.log(options,"options") //{id: "23", name: "zs"} },