赞
踩
页面路由指在应用程序中实现不同页面之间的跳转和数据传递。HarmonyOS提供了Router模块,通过不同的url地址,可以方便地进行页面路由,轻松地访问不同的页面。
Standard:标准实例模式,也是默认情况下的实例模式。每次调用该方法都会新建一个目标页,并压入栈顶。
Single:单实例模式。即如果目标页的url在页面栈中已经存在同url页面,则离栈顶最近的同url页面会被移动到栈顶,并重新加载;如果目标页的url在页面栈中不存在同url页面,则按照标准模式跳转。
import router from '@ohos.router';
场景1:登录页跳转到首页,需要保留主页在页面栈中,以便返回时恢复状态。
使用pushUrl()方法,并且使用Standard实例模式
- import router from '@ohos.router'
- @Entry
- @Component
- struct LoginPage {
- @State message: string = 'login'
-
- build() {
- Row() {
- Column({space:6}) {
- Text(this.message)
- .fontSize(50)
- .fontWeight(FontWeight.Bold)
- Button('跳转到home页面--pushUrl')
- .fontSize(20)
- .onClick(() =>{
- router.pushUrl(
- {
- url:'pages/HomePage'
- },
- router.RouterMode.Standard,
- (err) => {
- if(err){
- console.log('路由失败')
- }
- }
- )
- })
- }
- .width('100%')
- }
- .height('100%')
- }
- }
场景1中,登录跳转到首页时,需要保证每次首页存在于页面栈中,在返回时直接回到登录页。 需要将示例模式更换成Single,即:router.RouterMode.Single
场景2:登录页跳转到首页,销毁登录页,在返回时直接退出应用。
使用replaceUrl()方法,并且使用Standard实例模式
- import router from '@ohos.router'
- @Entry
- @Component
- struct LoginPage {
- @State message: string = 'login'
-
- build() {
- Row() {
- Column({space:6}) {
- Text(this.message)
- .fontSize(50)
- .fontWeight(FontWeight.Bold)
- Button('跳转到home页面--replaceUrl')
- .fontSize(20)
- .onClick(() =>{
- router.replaceUrl(
- {
- url:'pages/HomePage'
- },
- router.RouterMode.Standard,
- (err) => {
- if(err){
- console.log('路由失败')
- }
- }
- )
- })
- }
- .width('100%')
- }
- .height('100%')
- }
- }
场景2中,登录跳转到首页时,如果已经存在了就不再新建首页,直接跳转到首页。 需要将示例模式更换成Single,即:router.RouterMode.Single
如果需要在跳转时传递一些数据给目标页,则可以在调用Router模块的方法时,添加一个params属性,并指定一个对象作为参数。例如:
- Button('跳转到home页面--pushUrl')
- .fontSize(20)
- .onClick(() =>{
- router.pushUrl(
- {
- url:'pages/HomePage',
- params:{id:1}
- },
- router.RouterMode.Standard,
- (err) => {
- if(err){
- console.log('路由失败')
- }
- }
- )
- })
在目标页中,可以通过调用Router模块的getParams()方法来获取传递过来的参数。例如:
- const params = router.getParams(); // 获取传递过来的参数对象
- const id = params['id']; // 获取id属性的值
router.back();
- router.back({
- url: 'pages/Info'
- });
这种方式可以返回到指定页面,需要指定目标页的路径。目标页必须存在于页面栈中才能够返回。
- router.back({
- url: 'pages/Info',
- params: {
- id:1
- }
- });
这种方式不仅可以返回到指定页面,还可以在返回的同时传递自定义参数信息。这些参数信息可以在目标页中通过调用router.getParams()方法进行获取和解析。
在目标页中,在需要获取参数的位置调用router.getParams()方法即可,例如在onPageShow()生命周期回调中:
- onPageShow() {
- const params = router.getParams(); // 获取传递过来的参数对象
- const id = params['id']; // 获取id属性的值
- }
在开发应用时,为了避免用户误操作或者丢失数据,有时候需要在用户从一个页面返回到另一个页面之前,弹出一个询问框,让用户确认是否要执行这个操作。
如果想要在目标界面开启页面返回询问框,需要在调用router.back()方法之前,通过调用router.showAlertBeforeBackPage()方法设置返回询问框的信息。
例如,在Home页面中定义一个返回按钮的点击事件处理函数,点击按钮时弹出询问框,点击确定按钮再返回到登录页
- import router from '@ohos.router'
- @Entry
- @Component
- struct HomePage {
- @State message: string = 'Home'
- @State params:any = router.getParams()
-
- build() {
- Row() {
- Column({space:6}) {
- Text(this.message)
- .fontSize(50)
- .fontWeight(FontWeight.Bold)
- Text('接收login传递的id:' + this.params.id)
- .fontSize(30)
- .fontWeight(FontWeight.Bold)
- Button('返回到login页面')
- .fontSize(20)
- .onClick(() => {
- router.showAlertBeforeBackPage({
- message:'确定返回到login页面吗?'
- })
- router.back()
- })
- }
- .width('100%')
- }
- .height('100%')
- }
- }
其中,router.showAlertBeforeBackPage()方法接收一个对象作为参数,该对象包含以下属性:
当用户点击“返回”按钮时,会弹出确认对话框,询问用户是否确认返回。选择“取消”将停留在当前页目标页;选择“确认”将触发router.back()方法,并根据参数决定如何执行跳转。
点击按钮时,调用弹窗的promptAction.showDialog()方法:
- import router from '@ohos.router'
- import promptAction from '@ohos.promptAction'
- @Entry
- @Component
- struct HomePage {
- @State message: string = 'Home'
- @State params:any = router.getParams()
-
- build() {
- Row() {
- Column({space:6}) {
- Text(this.message)
- .fontSize(50)
- .fontWeight(FontWeight.Bold)
- Text('接收login传递的id:' + this.params.id)
- .fontSize(30)
- .fontWeight(FontWeight.Bold)
- Button('返回到login页面')
- .fontSize(20)
- .onClick(() => {
- promptAction.showDialog({
- message:'确定返回到login页面吗?',
- buttons: [
- {
- text: '取消',
- color: '#FF0000'
- },
- {
- text: '确认',
- color: '#0099FF'
- }
- ]
- }).then((result)=>{
- if(result.index === 0){
- // 用户点击了“取消”按钮
- console.log('点击了取消按钮')
- }else if(result.index === 1){
- // 用户点击了“确认”按钮
- console.log('用户点击了“确认”按钮')
- // 调用router.back()方法,返回上一个页面
- router.back();
- }
- }).catch(err => {
- console.error(`Invoke showDialog failed, code is ${err.code}, message is ${err.message}`);
- })
- })
- }
- .width('100%')
- }
- .height('100%')
- }
- }
当用户点击“返回”按钮时,会弹出自定义的询问框,询问用户是否确认返回。选择“取消”将停留在当前页目标页;选择“确认”将触发router.back()方法,并根据参数决定如何执行跳转。
最后:
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/220989
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。