小程序页面路由整理
小程序跳转功能以及参数传递
前言
最近在做小程序开发时,遇到了一些问题,比如页面跳转以及页面传参,这里总结一下,方便以后查阅。
-、几种页面跳转方式
- wx.switchTab
- wx.navigateTo
- wx.redirectTo
- wx.reLaunch
- wx.navigateBack
- 自定义路由
1、wx.switchTab 跳转 tabBar 定义的页面
使用示例
app.json 配置
{
"tabBar": {
"list": [
{
"pagePath": "pages/index/index",
"text": "首页"
},
{
"pagePath": "pages/logs/logs",
"text": "日志"
}
]
}
}
代码示例
wx.switchTab({
url: '/pages/index/index', // 注意路径必须为 tabBar 中的路径
success: function () { // 调用成功回调函数
console.log('跳转成功')
},
fail: function () { // 调用失败回调函数
console.log('跳转失败')
},
complete: function () { // 调用完成回调函数
console.log('跳转完成')
}
})
2、wx.navigateTo 跳转到应用内的某个页面,但是不能跳到 tabbar 页面
使用示例
app.json 配置
{
"pages": [
"pages/index/index",
"pages/logs/logs"
]
}
代码示例
a页面 JS 文件
wx.navigateTo({
url: '/pages/logs/logs?id=1', // 跳转路径 ?后为参数
success: function () { // 调用成功回调函数
console.log('跳转成功')
},
fail: function () { // 调用失败回调函数
console.log('跳转失败')
},
complete: function () { // 调用完成回调函数
onsole.log('跳转完成')
}
})
b页面 JS 文件
Page({
onLoad: function (options) { // options 为跳转路径携带的参数
console.log(options.id) // 输出 1
}
})
3、wx.redirectTo 关闭当前页面,跳转到应用内的某个页面。但是不允许跳转到 tabbar 页面
使用示例
app.json 配置
{
"pages": [
"pages/index/index",
"pages/logs/logs"
]
}
代码示例
a页面 JS 文件
wx.redirectTo({
url: '/pages/logs/logs?id=1', // 跳转路径 ?后为参数 非 tabBar 页面
success: function () { // 调用成功回调函数
console.log('跳转成功')
},
fail: function () { // 调用失败回调函数
console.log('跳转失败')
},
complete: function () { // 调用完成回调函数
console.log('跳转完成')
}
})
b页面 JS 文件
Page({
onLoad: function (options) { // options 为跳转路径携带的参数
console.log(options.id) // 输出 1
}
})
4、wx.reLaunch 关闭所有页面,打开到应用内的某个页面。但是不允许跳转到 tabbar 页面
使用示例
app.json 配置
{
"pages": [
"pages/index/index",
"pages/logs/logs"
]
}
代码示例
a页面 JS 文件
wx.reLaunch({
url: '/pages/logs/logs?id=1', // 跳转路径 ?后为参数 非 tabBar 页面
success: function () { // 调用成功回调函数
console.log('跳转成功')
},
fail: function () { // 调用失败回调函数
console.log('跳转失败')
},
complete: function () { // 调用完成回调函数
console.log('跳转完成')
}
})
b页面 JS 文件
Page({
onLoad: function (options) { // options 为跳转路径携带的参数
console.log(options.id) // 输出 1
}
})
5、wx.navigateBack 关闭当前页面,返回上一页面或多级页面。可以返回多级页面,返回的页面由开发者调用 navigateTo 或 redirectTo 而来
使用示例
a页面 JS 文件
// 此处是A页面
wx.navigateTo({
url: 'B?id=1'
})
// 此处是B页面
wx.navigateTo({
url: 'C?id=1'
})
// 此处是C页面
const pageList = getCurrentPages() // 获取当前页面栈, 通过 pageList 长度判断返回页面层级
wx.navigateBack({
delta: 1, // 返回的页面层级
success: function () { // 调用成功回调函数
console.log('跳转成功')
},
fail: function () { // 调用失败回调函数
console.log('跳转失败')
},
complete: function () { // 调用完成回调函数
console.log('跳转完成')
}
})
6.自定义路由
wx.router
app.json 配置
{
"pages": [
"pages/index/index",
"pages/logs/logs"
],
"renderer": "webview",
"rendererOptions": {
"skyline": {
"defaultDisplayBlock": true,
"disableABTest": true,
"sdkVersionBegin": "3.0.0",
"sdkVersionEnd": "15.255.255"
}
},
"componentFramework": "glass-easel",
}
代码示例
a页面 JS 文件
// 定义自定义效果,从右侧推入
const {windowWdith, screenHeight} = wx.getWindowInfo()
const slideRouteBuilder = (customRouteContext) => {
const { primaryAnimation } = customRouteContext
const handlePrimaryAnimation = () => {
'worklet'
const transX = windowWidth * (1 - primaryAnimation.value)
return {
transform: `translateX(${transX}px)`,
}
}
return {
handlePrimaryAnimation
}
}
wx.router.addRouteBuilder('slide', slideRouteBuilder)
// 使用自定义路由
wx.navigateTo({
url: 'xxx',
routeType: 'slide'
})
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构