路由跳转
1、uni.navigateTo(OBJECT)
保留当前页面(跳转新的页面),跳转到应用内的某个页面,使用uni.navigateBack可以返回到原页面(非tabBar注册页面使用该方法)
methods: {
toPath(){
//跳转非pages.json>tabbar>配置过的页面,使用navigateTo
//跳转时保留老页面,一般用于需要返回
uni.navigateTo({
url:"../one/one"
})
}
}
uni.navigateTo(object)必填参数url
其他参数:
传参:
//在起始页面跳转到test.vue页面并传递参数
uni.navigateTo({
url: 'test?id=1&name=uniapp'
});
在test.vue页面接受参数
export default {
onLoad: function (option) { //option为object类型,会序列化上个页面传递的参数
console.log(option.id); //打印出上个页面传递的参数。
console.log(option.name); //打印出上个页面传递的参数。
}
}
2、uni.redirectTo(OBJECT)
关闭当前页面,跳转到应用内的某个页面。(url参数必须填写)
实例:
methods: {
toPath(){
uni.redirectTo({
url:"../two/two"//关闭当前页面跳转到two页面
})
}
}
其他参数:
3、uni.reLaunch(OBJECT)
关闭所有页面,打开到应用内的某个页面。(url参数必须填写)
其他参数:
uni.reLaunch({
url: 'test?id=1'//带参数(如果跳转的页面路径是 tabBar 页面则不能带参数)
});
export default {
onLoad: function (option) {
console.log(option.id);
}
}
4、uni.switchTab(OBJECT)
跳转到 tabBar 页面,并关闭其他所有非 tabBar 页面(url参数必须填写)
跳转到 tabBar 页面只能使用 switchTab 跳转
methods: {
toPath(){
//跳转pages.json>tabbar>配置过的页面,使用switchTab
uni.switchTab({
url:"../news/news"//需要跳转的 tabBar 页面的路径(需在 pages.json 的 tabBar 字段定义的页面),路径后不能带参数
})
}
}
5、uni.navigateBack(OBJECT)
关闭当前页面,返回上一页面或多级页面。可通过 getCurrentPages() 获取当前的页面栈,决定需要返回几层。
// 此处是A页面
uni.navigateTo({
url: 'B?id=1'
});
// 此处是B页面
uni.navigateTo({
url: 'C?id=1'
});
// 在C页面内 navigateBack,将返回A页面
uni.navigateBack({
delta: 2
});
提示:
- navigateTo, redirectTo 只能打开非 tabBar 页面
- switchTab 只能打开 tabBar 页面
- reLaunch 可以打开任意页面