| 路由方式 | 觸發(fā)時(shí)機(jī) | 路由前頁(yè)面 | 路由后頁(yè)面 |
|---|---|---|---|
| 初始化 | 小程序打開的第一個(gè)頁(yè)面 | onLoad, onShow | |
| 打開新頁(yè)面 | 調(diào)用 API wx.navigateTo 或使用組件 | onHide | onLoad, onShow |
| 頁(yè)面重定向 | 調(diào)用 API wx.redirectTo 或使用組件 | onUnload | onLoad, onShow |
| 頁(yè)面返回 | 調(diào)用 API wx.navigateBack 或使用組件或用戶按左上角返回按鈕 | onUnload | onShow |
| Tab 切換 | 調(diào)用 API wx.switchTab 或使用組件 或用戶切換 Tab | 各種情況請(qǐng)參考下表 | |
| 重啟動(dòng) | 調(diào)用 API wx.reLaunch 或使用組件 | onUnload | onLoad, onShow |
用法如下:
<navigator url="/pages/detail/detail" open-type='navigate' style='border:1px solid red;' >進(jìn)入非tab頁(yè)</navigator> <navigator url="/pages/index/index" open-type='switchTab' >進(jìn)入首頁(yè)(tab頁(yè))</navigator> <navigator url="/pages/index/index" open-type='switchTab' >進(jìn)入首頁(yè)(tab頁(yè))</navigator> <navigator open-type='navigateBack' >回退</navigator>
從列表頁(yè)進(jìn)入詳情頁(yè)時(shí),需要傳遞列表被點(diǎn)擊項(xiàng)目的 id 至詳情頁(yè),方法:
<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>
// 進(jìn)入詳情頁(yè)時(shí) 傳遞 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); // 拿到列表頁(yè)傳過(guò)來(lái)的 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();
}
})
},
})