您的位置:首页 > 移动开发 > 微信开发

微信小程序遇到的坑系列---微信小程序页面跳转的5种方式及区别

2018-08-28 14:15 246 查看

1.wx.navigateTo(OBJECT)

需要跳转的应用内非 tabBar 的页面的路径 , 路径后可以带参数。参数与路径之间使用

?
分隔,参数键与参数值用
=
相连,不同参数用
&
分隔;如 'path?key=value&key2=value2'

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

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

2.wx.redirectTo(OBJECT)

需要跳转的应用内非 tabBar 的页面的路径,路径后可以带参数。参数与路径之间使用

?
分隔,参数键与参数值用
=
相连,不同参数用
&
分隔;如 'path?key=value&key2=value2'

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

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

3.wx.reLaunch(OBJECT)

需要跳转的应用内页面路径 , 路径后可以带参数。参数与路径之间使用

?
分隔,参数键与参数值用
=
相连,不同参数用
&
分隔;如 'path?key=value&key2=value2',如果跳转的页面路径是 tabBar 页面则不能带参数

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

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

4.wx.switchTab(OBJECT)

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

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

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

4.wx.navigateBack(OBJECT)

参数 类型 默认值 说明
delta Number 1 返回的页面数,如果 delta 大于现有页面数,则返回到首页。

 

[code]// 此处是A页面
wx.navigateTo({
url: 'B?id=1'
})

// 此处是B页面
wx.navigateTo({
url: 'C?id=1'
})

// 在C页面内 navigateBack,将返回A页面
wx.navigateBack({
delta: 2
})

5.navigator 组件使用跳转

属性名 类型 默认值 说明 最低版本
target String   在哪个目标上发生跳转,默认当前小程序 2.0.7
url String   当前小程序内的跳转链接  
open-type String navigate 跳转方式  
delta Number   当 open-type 为 'navigateBack' 时有效,表示回退的层数  
app-id String   当target="miniProgram"时有效,要打开的小程序 appId 2.0.7
path String   当target="miniProgram"时有效,打开的页面路径,如果为空则打开首页 2.0.7
extra-data Object   当target="miniProgram"时有效,需要传递给目标小程序的数据,目标小程序可在 
App.onLaunch()
App.onShow()
 中获取到这份数据。详情
2.0.7
version version release 当target="miniProgram"时有效,要打开的小程序版本,有效值 develop(开发版),trial(体验版),release(正式版),仅在当前小程序为开发版或体验版时此参数有效;如果当前小程序是正式版,则打开的小程序必定是正式版。 2.0.7
hover-class String navigator-hover 指定点击时的样式类,当
hover-class="none"
时,没有点击态效果
 
hover-stop-propagation Boolean false 指定是否阻止本节点的祖先节点出现点击态 1.5.0
hover-start-time Number 50 按住后多久出现点击态,单位毫秒  
hover-stay-time Number 600 手指松开后点击态保留时间,单位毫秒  
bindsuccess String   当target="miniProgram"时有效,跳转小程序成功 2.0.7
binderror String   当target="miniProgram"时有效,跳转小程序失败 2.0.7
bindcomplete String   当target="miniProgram"时有效,跳转小程序完成 2.0.7

open-type 有效值:

说明 最低版本
navigate 对应 
wx.navigateTo
 或 
wx.navigateToMiniProgram
 的功能
 
redirect 对应 
wx.redirectTo
 的功能
 
switchTab 对应 
wx.switchTab
 的功能
 
reLaunch 对应 
wx.reLaunch
 的功能
1.1.0
navigateBack 对应 
wx.navigateBack
 的功能
1.1.0
exit 退出小程序,target="miniProgram"时生效 2.1.0

 

[code]/** wxss **/
/** 修改默认的navigator点击态 **/
.navigator-hover {
color:blue;
}
/** 自定义其他点击态样式类 **/
.other-navigator-hover {
color:red;
}
[code]<!-- 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>
[code]<!-- navigator.wxml -->
<view style="text-align:center"> {{title}} </view>
<view> 点击左上角返回回到之前页面 </view>
[code]<!-- redirect.wxml -->
<view style="text-align:center"> {{title}} </view>
<view> 点击左上角返回回到上级页面 </view>

最后,小程序页面之间的跳转用以上几个api是完全足够的,但是如果小程序与小程序之间的跳转,就需要用到组件了

具体的用法,参考微信小程序的文档就好了,已经非常的详细了

本文章的内容来自微信官方文档

https://developers.weixin.qq.com/miniprogram/dev/component/navigator.html

 

 

 

 

 

 

阅读更多
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: