当前位置:网站首页>微信小程序页面路由
微信小程序页面路由
2022-04-22 06:37:00 【陆荣涛】
微信小程序——页面路由
什么是路由?
路由(routing)是指分组从源到目的地时,决定端到端路径的网络范围的进程。我们可以理解微信小程序页面路由,根据路由规则(路径)从一个页面跳转到另一个页面的的规则。
一、哪些会触发页面跳转
- 小程序启动,初始化第一个页面
- 跳转新页面,调用
wx.navigateTo或者<navigator /> - 页面重定向,调用
wx.redirectTo或者<navigator /> - 页面返回,调用
wx.navigateBack,页面左上角返回按钮 wx.switchTab实现tabBar页面切换
Tips: 所有页面都必须在app.json中注册,例如
{
"pages": [
"pages/index/index",
"pages/classification/classification",
"pages/start/start",
"pages/detail/detail",
]
}
二、微信小程序中实现页面路由的几种方式
wx.navigateTo,保留当前页面,跳转到应用内的某个页面,但是不能跳到 tabbar 页面
wx.navigateTo({
url: 'pages/detail/detail',
success: function(res) {},
...
})
wx.redirectTo,关闭当前页面,跳转到应用内的某个页面,但是不允许跳转到 tabbar 页面
wx.redirectTo({
url: 'pages/detail/detail',
success:function(res){},
...
})
<navigator />组件跳转方式
<navigator url=pages/detail/detail">跳转</navigator>
wx.navigateBack返回上一页
wx.navigateBack({
delta: 1,
})
Tips: delta为1时表示返回上一页,为2时表示上上一页,以此类推;如果dalta大于已打开的页面总数,则返回到首页。返回后,元界面会销毁
wx.switchTab跳转到 tabBar 页面,并关闭其他所有非 tabBar 页面
app.json:
{
"tabBar": {
"list": [{
"pagePath": "pages/index/index",
"text": "首页",
},
{
"pagePath": "pages/car/car",
"text": "购物车",
},
...
}
}
index.js:
wx.switchTab({
url: 'pages/car/car'
})
三、小程序路由实现原理
小程序路由是通过自己实现的一个栈(先进先出)来管理的。
当我们通过wx.navigateTo或者<navigator/>从A页面跳转到B页面时。路由栈的变化是这样的。

路由栈刚开始只存有页面A,当使用wx.navigateTo跳转后,页面B推入路由栈并展示到界面上,页面A隐藏。
当我们使用wx.navigateBack返回时

那么wx.redirectTo与wx.navigateTo有什么区别呢?
假如当前已经在二级页面B上,我们使用wx.redirectTo跳转到C页面,其过程是这样的。

如当前已经在二级页面B上,我们使用wx.redirectTo跳转到C页面,其过程是这样的。
[外链图片转存中…(img-mkPnbKug-1650431194878)]
页面B会被pop出,然后C页面再push进入栈,这个时候栈中还是只有两个页面。
*获取千锋教育学习视频资料+源码笔记 ,进学习交流群
请添加下方微信(备注CSDN推荐)

版权声明
本文为[陆荣涛]所创,转载请带上原文链接,感谢
https://blog.csdn.net/sdasadasds/article/details/124295021
边栏推荐
- Postman interface automation-4-tests assertion 4: custom interface execution order
- mysql数据库,索引字符集不一致,slowsql慢查询,接口超时问题
- 判断所给年份中是否有闰年
- JMeter parameter request type
- jmeter 参数请求类型
- 学生成绩管理
- Shiping information participated in the enterprise roadshow of Hengyang "Chuanshan forum", talked about data security and helped collaborative innovation
- js 实现new
- Use SSS framework to build a simple dormitory management system
- web自动化:8.2 selenium中如何发送js指令
猜你喜欢

通过OpenFeign传递对象类型参数

The data security product guide was officially released, and many products of Shiping information were included

Help 2021 Hangzhou network security publicity week | collection of wonderful activities of Shiping information

JMeter simulates get requests

laravel8-使用jwt

laravel 新建路由文件

JMeter post request

TP5 发送邮件(2020-05-27)

jmeter_mysql 数据库连接

Shrio 学习笔记(一)
随机推荐
MYSQL04_算术、逻辑、位、运算符、运算符对应的习题
Web automation: how to send JS instructions in 8.2 selenium
华为手机adb devices连接设备为空
Monkey actual combat
Web automation: 6 Operation of selenium drop-down selection box - Select
学生成绩管理
How to connect Kunlun on state │ G series screen with Siemens 300 domestic MPI adapter
递归-输出连续递增的数
ADB advanced commands
Web automation: 4.2 selenium how to switch iframe windows (3 methods)
【ncnn】——param中-23300的意思
jmeter post 请求
Why do I value document naming so much?
pytest_第一节课
postman中级:处理接口加解密 AES SM3 SM4
Seven steps of PLC project commissioning
Use SSS framework to build a simple dormitory management system
adb 高级部分命令
node配置环境cmd不生效
JMeter parameter request type