1.什么是页面导航
页面导航指的是页面之间的相互跳转。
2.小程序中实现页面导航的两种方式
声明式导航
在页面上声明一个<navigator>导航组件
通过点击<navigator> 组件实现页面跳转
编程式导航
调用小程序的导航API,实现页面的跳转。
声明式导航
导航到tabBar页面
tabBar页面指的是被配置为tabBar的页面。
在使用<navigator>组件跳转到指定的tabBar页面时,需要指定url属性和open-type属性,其中: url表示要跳转的页面的地址,必须以/开头
open-type表示跳转的方式,值必须为switchTab
<navigator url="路径名" open-type="switchTab">点击跳转到指定页面</navigator>
导航到非tabBar页面
非tabBar页面指的是没有被配置为tabBar的页面。
在使用<navigator>组件跳转到普通的非tabBar页面时,则需要指定url属性和open-type属性,其中:url表示要跳转的页面的地址,必须以/开头
open-type表示跳转的方式,必须为navigate(在导航到非tabBar页面时,open-type属性可以省略)
<navigator url="路径名" open-type="navigate">点击跳转到指定页面</navigator>
后退导航
如果要后退到上一页面或多级页面,则需要指定open-type属性和delta 属性,其中:
open-type的值必须是navigateBack,表示要进行后退导航
delta的值必须是数字,表示要后退的层级(默认为1)
<navigator open-type="navigateBack" delta="1">点击返回上一级页面</navigator>
编程式导航
导航到tabBar页面
<button type="primary" bindtap="btp">点击跳转到首页</button>
btp(){
wx.switchTab({
url: '/pages/index/index'
})
},
导航到非tabBar页面
<button type="primary" bindtap="btp2">点击跳转到非tabBar页面</button>
btp2(){
wx.navigateTo({
url: '/pages/demo2/demo2',
})
},
后退导航
<button type="primary" bindtap="btp3">点击返回上一页</button>
btp3(){
wx.navigateBack({
delta : 1
})
},
导航传参
1.声明式导航传参
navigator组件的url属性用来指定将要跳转到的页面的路径。同时,路径的后面还可以携带参数:
参数与路径之间使用?分隔
参数键与参数值用=相连不同参数用&分隔
<navigator url="/pages/demo2/demo2?name=zs&age=20">点击跳转并传参</navigator>
2.编程式传参
<button type="primary" bindtap="btp2">点击跳转到非tabBar页面</button>
btp2(){
wx.navigateTo({
url: '/pages/demo2/demo2?name=zs&age=20',
})
},
3.在onload接收参数
注意:给哪个网页传递参数就在对应的.js文件上写
onLoad :function(options){
console.log(options)
},
4.接收参数并传递到data中
data: {
query:{}
},
onLoad :function(options){
console.log(options)
this.setData({
query : options
})
},