一、利用小程序提供的 API 跳轉(zhuǎn):
1.// 保留當(dāng)前頁(yè)面呛凶,跳轉(zhuǎn)到應(yīng)用內(nèi)的某個(gè)頁(yè)面,使用wx.navigateBack可以返回到原頁(yè)面。
// 注意:調(diào)用 navigateTo 跳轉(zhuǎn)時(shí)弹谁,調(diào)用該方法的頁(yè)面會(huì)被加入堆棧,但是 redirectTo
wx.navigateTo({
url: 'page/home/home?user_id=111'
})
2.// 關(guān)閉當(dāng)前頁(yè)面句喜,返回上一頁(yè)面或多級(jí)頁(yè)面预愤。可通過(guò) getCurrentPages() 獲取當(dāng)前的頁(yè)面棧咳胃,決定需要返回幾層植康。
wx.navigateTo({
url: 'page/home/home?user_id=111' // 頁(yè)面 A
})
wx.navigateTo({
url: 'page/detail/detail?product_id=222' // 頁(yè)面 B
})
// 跳轉(zhuǎn)到頁(yè)面 A
wx.navigateBack({
delta: 2
})
3.// 關(guān)閉當(dāng)前頁(yè)面,跳轉(zhuǎn)到應(yīng)用內(nèi)的某個(gè)頁(yè)面展懈。
wx.redirectTo({
url: 'page/home/home?user_id=111'
})
4.// 跳轉(zhuǎn)到tabBar頁(yè)面(在app.json中注冊(cè)過(guò)的tabBar頁(yè)面)销睁,同時(shí)關(guān)閉其他非tabBar頁(yè)面。
wx.switchTab({
url: 'page/index/index'
})
5.// 關(guān)閉所有頁(yè)面存崖,打開(kāi)到應(yīng)用內(nèi)的某個(gè)頁(yè)面冻记。
wx.reLanch({
url: 'page/home/home?user_id=111'
})
二、 wxml 頁(yè)面組件跳轉(zhuǎn)(可以通過(guò)設(shè)置open-type屬性指明頁(yè)面跳轉(zhuǎn)方式):
// navigator 組件默認(rèn)的 open-type 為 navigate
<navigator url="/page/navigate/navigate?title=navigate" hover-class="navigator-hover">跳轉(zhuǎn)到新頁(yè)面</navigator>
// redirect 對(duì)應(yīng) API 中的 wx.redirect 方法
<navigator url="../../redirect/redirect/redirect?title=redirect" open-type="redirect" hover-class="other-navigator-hover">在當(dāng)前頁(yè)打開(kāi)</navigator>
// switchTab 對(duì)應(yīng) API 中的 wx.switchTab 方法
<navigator url="/page/index/index" open-type="switchTab" hover-class="other-navigator-hover">切換 Tab</navigator>
// reLanch 對(duì)應(yīng) API 中的 wx.reLanch 方法
<navigator url="../../redirect/redirect/redirect?title=redirect" open-type="redirect" hover-class="other-navigator-hover">關(guān)閉所有頁(yè)面来惧,打開(kāi)到應(yīng)用內(nèi)的某個(gè)頁(yè)面</navigator>
// navigateBack 對(duì)應(yīng) API 中的 wx.navigateBack 方法
<navigator url="/page/index/index" open-type="navigateBack" hover-class="other-navigator-hover">關(guān)閉當(dāng)前頁(yè)面冗栗,返回上一級(jí)頁(yè)面或多級(jí)頁(yè)面</navigator>