微信小程序页面跳转的6种方式和区别

1.wx.navigateTo(OBJECT)

   需要跳转的应用内非 tabBar 的页面的路径 , 路径后可以带参数。参数与路径之间使用?分隔,参数键与参数值用=相连,不同参数用&分隔;如 ‘path?key=value&key2=value2’。

wx.navigateTo({
  url: 'test?id=1'
})

   这种跳转方式默认有返回按钮,返回到上一个页面。

2.wx.redirectTo(OBJECT)

   需要跳转的应用内非 tabBar 的页面的路径,路径后可以带参数。参数与路径之间使用?分隔,参数键与参数值用=相连,不同参数用&分隔;如 ‘path?key=value&key2=value2’。

wx.redirectTo({
  url: 'test?id=1'
})

   这种跳转方式默认有返回按钮,返回到上一个页面的再上一层。

3.wx.reLaunch(OBJECT)

   需要跳转的应用内页面路径 , 路径后可以带参数。参数与路径之间使用?分隔,参数键与参数值用=相连,不同参数用&分隔;如 ‘path?key=value&key2=value2’,如果跳转的页面路径是 tabBar 页面则不能带参数。

wx.reLaunch({
  url: 'test?id=1'
})

    这种跳转方式默认没有返回按钮,不需要默认返回按钮的页面就可以使用这个api了。

4.wx.switchTab(OBJECT)

   需要跳转的 tabBar 页面的路径(需在 app.json 的 tabBar 字段定义的页面),路径后不能带参数。

{
  "tabBar": {
    "list": [{
      "pagePath": "index",
      "text": "首页"
    },{
      "pagePath": "other",
      "text": "其他"
    }]
  }
}
wx.switchTab({
  url: '/index'
})

    我们需要调转到tabbar定义的页面的时候,就需要这个api了。踩过这个坑的人就知道,除了这个api,其他的都不能跳转到tabar定义过的页面

5.wx.navigateBack(OBJECT)

   delta Number 1 返回的页面数,如果 delta 大于现有页面数,则返回到首页。

// 此处是A页面
wx.navigateTo({
 url: 'B?id=1'
})
// 此处是B页面
wx.navigateTo({
 url: 'C?id=1'
})
// 在C页面内 navigateBack,将返回A页面
wx.navigateBack({
 delta: 2
})

6.navigator 组件使用跳转

/** wxss **/
/** 修改默认的navigator点击态 **/
.navigator-hover {
	color:blue;
}
/** 自定义其他点击态样式类 **/
.other-navigator-hover {
	color:red;
}
<!-- sample.wxml -->
<view class="btn-area">
  <navigator url="/page/navigate/navigate?title=navigate" hover-class="navigator-hover">跳转到新页面</navigator>
  <navigator url="../../redirect/redirect/redirect?title=redirect" open-type="redirect" hover-class="other-navigator-hover">在当前页打开</navigator>
  <navigator url="/page/index/index" open-type="switchTab" hover-class="other-navigator-hover">切换 Tab</navigator>
  <navigator target="miniProgram" open-type="navigate" app-id="" path="" extra-data="" version="release">打开绑定的小程序</navigator>
</view>
<!-- navigator.wxml -->
<view style="text-align:center"> {{title}} </view>
<view> 点击左上角返回回到之前页面 </view>
<!-- redirect.wxml -->
<view style="text-align:center"> {{title}} </view>
<view> 点击左上角返回回到上级页面 </view>
发布了28 篇原创文章 · 获赞 38 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/qq_36158551/article/details/105284693