发布于 4年前

微信小程序页面跳转的几种方法

本文总结了微信小程序页面跳转分为这几种类型:

  • navigate:跳转页面,并保留当前页面。前一个页面可以使用navigateBack类型的跳转退回。
  • redirect:关闭当前页面,然后跳转到新页面。
  • switchTab:跳转到tabBar页面(在app.json中注册过的tabBar页面),同时关闭其他非tabBar页面。
  • navigateBack:页面退回。

微信原生提供的API

navigateTo

wx.navigateTo({
  url: 'page/home'
})

navigateBack

wx.navigateTo({
 url: 'page/page_a'
})

wx.navigateTo({
 url: 'page/page_b'
})

// 跳转到page_a
wx.navigateBack({
 delta: 2
})

redirectTo

wx.redirectTo({
  url: 'page/home'
})

switchTab

wx.switchTab({
  url: 'page/index/index'
})

wxml页面组件跳转

在wxml使用open-type来设置页面的跳转类型。其中open-type默认值为navigate

<navigator url="/page/navigate">跳转到新页面</navigator>
<navigator url="/page/redirect" open-type="redirect">在当前页打开</navigator>
<navigator url="/page/index" open-type="switchTab">Tab切换</navigator>
<navigator url="/page/index" open-type="navigateBack">返回/navigator>
©2020 edoou.com   京ICP备16001874号-3