【微信小程序】页面导航详解

🖐本节学习目标:✅学会如何使用页面之间的导航跳转
一.页面导航
什么是页面导航
<font color=blue>页面导航</font>是指页面之间的相互跳转。
2.页面导航的两种实现方式
页面导航有两种实现方式:
<font color=blue>声明式导航</font>
方法:在页面声明一个 navigator 组件,通过点击这个组件来实现页面的跳转。
<font color=blue>编程式导航</font>
方法:调用小程序的导航 API 实现页面的跳转。
二.声明式导航
1.导航到 tabBar 页面
tab 页面指被配置为 tabBar 的页面,在使用指定 navigator 组件跳转到指定页面时,必需要指定 url 属性和 open-style 属性,其中:
url
表示要跳转的页面地址,必须以/开头open-strle
表示跳转的方式,这里必须是switchTab

2.导航到非 tabBar 页面
非 tabBar 页面指的是没有被配置为 tabBar 的页面。在使用 naxigator 组件跳转到非 tabBar 页面时,也需要指定 url 属性和 open-type 属性,其中:
url
表示要跳转的页面地址,必须以/开头open-style
表示跳转的方式,这里必须是navigate
,此时这个属性可以省略

3.后退导航
如果要后退到上一页面或多级页面,则需要指定 open-type 属性和 delta 属性,其中:
open-type
的值必须是 navigateBack,表示要进行后退导航delta
的值必须是数字,表示要后退的层级,默认为 1

三.编程式导航
1.导航到 tabBar 页面
调用 ==wx.switchTab(Object object)== 方法,可以跳转到 tabBar 页面。其中 Object 参数对象的属性列表在官方文档解释如下:

wxml 代码:
js 代码:

2.导航到非 tabBar 页面
调用==wx.navigateTo(Object object)== 方法,可以跳转到非 tabBar 的页面。其中 Object 参数对象的属性列表在官方文档解释如下:

wxml\代码:
js 代码:

3.后退导航
调用 ==wx.navigateBack(Object object)== 方法,可以返回上一页面或多级页面。其中 Object 参数对象可选的属性列表在官方文档解释如下:

wxml 代码:
js 代码:

四.导航传参
1.声明式导航传参
navigator
组件的 url 属性用来指定将要<fonr color=blue>跳转到的页面的路径</font>。同时,路径的后面还可以携带参数:
参数与路径之间使用 ? 分隔
参数键与参数值用 = 相连
不同参数用 & 分隔
2.编程式导航传参
调用 ==wx.navigateTo(Object object)== 方法跳转页面时,也可以携带参数。
3.在 onLoad 中接收导航传参
通过声明式导航传参或编程式导航传参所携带的参数,可以直接在 onLoad 事件中直接获取到。
五.总结
本节学会了页面导航的两种方式和两种方式导航到 tabBar 页面和非 tabBar 页面的方式,以及导航传参的方法。
版权声明: 本文为 InfoQ 作者【陈橘又青】的原创文章。
原文链接:【http://xie.infoq.cn/article/31237a6fbf3f7ee46312cbc14】。文章转载请联系作者。
评论