微信小程序之路由
2024-10-18 19:25:15
1. 路由方式
路由方式 | 触发时机 | 路由前页面 | 路由后页面 |
---|---|---|---|
初始化 | 小程序打开的第一个页面 | onLoad, onShow | |
打开新页面 | 调用 API wx.navigateTo 或使用组件 | onHide | onLoad, onShow |
页面重定向 | 调用 API wx.redirectTo 或使用组件 | onUnload | onLoad, onShow |
页面返回 | 调用 API wx.navigateBack 或使用组件或用户按左上角返回按钮 | onUnload | onShow |
Tab 切换 | 调用 API wx.switchTab 或使用组件 或用户切换 Tab | 各种情况请参考下表 | |
重启动 | 调用 API wx.reLaunch 或使用组件 | onUnload | onLoad, onShow |
用法如下:
<navigator url="/pages/detail/detail" open-type='navigate' style='border:1px solid red;' >进入非tab页</navigator>
<navigator url="/pages/index/index" open-type='switchTab' >进入首页(tab页)</navigator>
<navigator url="/pages/index/index" open-type='switchTab' >进入首页(tab页)</navigator>
<navigator open-type='navigateBack' >回退</navigator>
navigateTo
,redirectTo
只能打开非 abBar 页面。switchTab
只能打开 tabBar 页面。reLaunch
可以打开任意页面, 但是没有返回按钮,需要定义一个可以点击回退的按钮。- 页面底部的 tabBar 由页面决定,即只要是定义为 tabBar 的页面,底部都有 tabBar。
- 调用页面路由带的参数可以在目标页面的
onLoad
中获取。
2. 路由传参
从列表页进入详情页时,需要传递列表被点击项目的 id 至详情页,方法:
- 在列表页通过
data-id='{{item.id}}'
给各个项目绑定一个 id ; - 在详情页通过 onload 拿到 id;
列表页:
<view class="list" >
<view class='box' wx:for='{{list}}' wx:key='{{index}}' data-id='{{item.id}}' bindtap='goDetail'>
<image src='{{item.img}}'></image>
<view class='tip'>
<text>{{item.title}}</text>
<text class='price'>¥{{item.price}}</text>
</view>
</view>
</view>
// 进入详情页时 传递 id
goDetail (e) {
console.log(e.currentTarget.dataset.id),
wx.navigateTo({
url: `/pages/detail/detail?id=${e.currentTarget.dataset.id}`,
})
},
详情页:
// pages/detail/detail.js
Page({
data: {
detail: {},
loading: true
},
onLoad: function (options) {
this.loadDetail(options.id); // 拿到列表页传过来的 id
console.log(options.id)
},
loadDetail (id) {
wx.showLoading({
title: '详情加载中...',
})
wx.request({
url: 'http://10.0.1.109:3000/list',
success: (res) => {
console.log(res.data.cityList);
let thisPlace = res.data.cityList.filter((val) => val.id == id)
console.log(thisPlace)
this.setData({
detail: thisPlace[0],
loading: false
});
console.log(this.data.detail)
wx.hideLoading();
}
})
},
})
最新文章
- sun.misc.BASE64Encoder找不到jar包的解决方法
- Maven pom.xml中添加指定的中央仓库
- Facebook内部分享:26个高效工作的小技巧
- IE9或以上的浏览器flash值为空时,导致domready不触发
- CSS简写指南
- JQgrid for asp.net
- 解析网络json数据,模拟美团界面显示。
- 谷歌 不支持 activeX插件
- 不相交集(The Disjoint Set ADT)
- -_-#【JS】defer / async
- Linux vsftpd 无法登录 cannot change directory:xxx priv_sock_get_cmd 问题
- UVa 1637 (概率) Double Patience
- NVMe 与 AHCI
- fuel iso光盘刻录机usb Driver 烧录
- object detection技术演进:RCNN、Fast RCNN、Faster RCNN
- Java报错信息 java.lang.SecurityException: Prohibited package name: java.xxx
- [蓝桥杯]2015蓝桥省赛B组题目及详解
- Linux上svn搭建
- 严选 | Elasticsearch史上最全最常用工具清单【转】
- JavaScript按纯数字排序
热门文章
- mysql Alter table设置default的问题,是bug么?
- man -k : nothing appropriate.
- 【转】Python学习---Socket通信原理以及三次握手和四次挥手详解
- django版本切换以及更改url(pycharm)
- HDFS NameNode内存详解
- 中国将有可能在全球化的背景下收获新的人口红利:3星|《<;财经>;2019:预测与战略》
- 使用Python+opencv2时的文件命名及路径问题
- Microsoft.ACE.OLEDB.12.0' provider is not registered on the local machine
- buffers与cached
- div设置contenteditable=";true"; 光标消失:原因