页面跳转和两个页面之间的数据传递-鸿蒙ArkTS

打印 上一主题 下一主题

主题 1016|帖子 1016|积分 3048

页面跳转和两个页面之间的数据传递-ArkTS

   本篇文章主要是对两个页面之间数据传递进行实现。
  


关于router的利用

页面跳转和参数接受是通过导入 router 模块实现。
  1. import router from '@ohos.router';
复制代码


  • router.pushUrl() 跳转到指定页面。
  • router.replaceUrl() 替换当前页面并烧毁。
  • router.back() 返回上一个页面。
  • router.getParams() 获取上一个页面跳转过来携带的参数。
  • router.clear() 清空当前页面栈中全部汗青页面,只会保留当前页面作为栈顶页面。
  • router.getLength() 获取当前页面栈中的页面数目。
  • router.getState() 获取当前页面的状态信息。
  • router.showAlertBeforeBackPage() 开启页面返回询问对话框。
  • router.hideAlertBeforeBackPage() 禁用页面返回询问对话框。
跳转页面的实现方式。

   API9及以上,router.pushUrl()方法新增了mode参数,可以将mode参数配置为router.RouterMode.Single单实例模式和router.RouterMode.Standard多实例模式。
  router.pushUrl()方法内的基本参数
  1. router.pushUrl({
  2.         // 跳转页面路径
  3.         url: '页面',
  4.         // 跳转携带的参数
  5.         params: {
  6.                 变量名:值
  7.         }
  8. }, 当前模式)
复制代码
方式一
在多实例模式下:如果目标页面的url在页面栈中已经存在同url页面,离栈顶近来同url页面会被移动到栈顶,移动后的页面为新建页,原来的页面仍然存在栈中,页面栈的元素数目不变;如果目标页面的url在页面栈中不存在同url页面,按多实例模式跳转,页面栈的元素数目会加1。
  1. // params对象内的变量是可以自定义参数名的,任意名称都可以只要在接受的时候通过传递时过来的变量名称就可以的。
  2. router.pushUrl({
  3. url: 'pages/Second',
  4. params: {
  5.         name: 'Index页面传递'
  6. }
  7. }, router.RouterMode.Single)
复制代码
方式二
在单实例模式下:如果目标页面的url在页面栈中已经存在同url页面,离栈顶近来同url页面会被移动到栈顶,替换当前页面,并烧毁被替换的当前页面,移动后的页面为新建页,页面栈的元素数目会减1;如果目标页面的url在页面栈中不存在同url页面,按多实例模式跳转,页面栈的元素数目不变。
  1. // params对象内的变量是可以自定义参数名的,任意名称都可以只要在接受的时候通过传递时过来的变量名称就可以的。
  2. router.pushUrl({
  3. url: 'pages/Second',
  4. params: {
  5.         name: 'Index页面传递'
  6. }
  7. }, router.RouterMode.Standard)
复制代码
页面接受跳转传递的参数

通过 router.getParams() 方法获取页面传递过来的自定义参数。
  1. import router from '@ohos.router'
  2. @Entry
  3. @Component
  4. struct Second {
  5.     // 用于接受由页面跳转传递的参数名称
  6.   @State str: string = router.getParams()?.['name']
  7.         // 页面刷新展示
  8.     ....
  9.   
  10. }
复制代码
页面返回及携带参数

返回上一个页面
  1. router.back();
复制代码
返回指定页面
  1. router.back({url: 'pages/Index'});
复制代码
返回并携带参数
  1. // 需要在 router.back() 方法内类似 router.pushUrl() 的用法
  2. router.back({
  3. url: 'pages/Index',
  4. params: {
  5.         src: 'Second传回来的数据'
  6. }
  7. })
复制代码
吸取返回的参数
  1. import router from '@ohos.router'
  2. @Entry
  3. @Component
  4. struct Index {
  5.         //定义一个变量来接收返回的参数
  6.   @State src: string = ''
  7.     // 这是一个生命周期,当页面每次显示的时候都会调用
  8.   onPageShow(){
  9.       // 通过拿到 router 中名称为 src 的值
  10.     this.src = router.getParams()?.['src']
  11.   }
  12.   build() {
  13.     Row() {
  14.       Column() {
  15.           // 将返回接收到的值显示
  16.         Text(this.src)
  17.         Button('Next')
  18.           .width("90%")
  19.           .onClick(() => {
  20.             router.pushUrl({
  21.               url: 'pages/Second',
  22.               params: {
  23.                 name: 'Index页面传递'
  24.               }
  25.             }, router.RouterMode.Single)
  26.           })
  27.       }
  28.       .width('100%')
  29.     }
  30.     .height('100%').backgroundColor('#efefef')
  31.   }
  32. }
复制代码
效果



代码

Index页面

  1. import router from '@ohos.router'
  2. @Entry
  3. @Component
  4. struct Index {
  5.   @State message: string = 'Hello World'
  6.   @State src: string = ''
  7.   onPageShow(){
  8.     this.src = router.getParams()?.['src']
  9.   }
  10.   build() {
  11.     Row() {
  12.       Column() {
  13.         Text(this.message)
  14.           .fontSize(50)
  15.           .fontWeight(FontWeight.Bold)
  16.         Text(this.src)
  17.         Button('Next')
  18.           .width("90%")
  19.           .onClick(() => {
  20.             router.pushUrl({
  21.               url: 'pages/Second',
  22.               params: {
  23.                 name: 'Index页面传递'
  24.               }
  25.             }, router.RouterMode.Single)
  26.           })
  27.       }
  28.       .width('100%')
  29.     }
  30.     .height('100%').backgroundColor('#efefef')
  31.   }
  32. }
复制代码
Second页面

  1. import router from '@ohos.router'
  2. @Entry
  3. @Component
  4. struct Second {
  5.   @State message: string = 'Hello World'
  6.   @State str: string = router.getParams()?.['name']
  7.   build() {
  8.     Row() {
  9.       Column() {
  10.         Text(this.message)
  11.           .fontSize(50)
  12.           .fontWeight(FontWeight.Bold)
  13.         Text(this.str)
  14.         Button('back')
  15.           .onClick(() => {
  16.             router.back({
  17.               url: 'pages/Index',
  18.               params: {
  19.                 src: 'Second传回来的数据'
  20.               }
  21.             })
  22.           })
  23.           .width("90%")
  24.       }
  25.       .width('100%')
  26.     }
  27.     .height('100%').backgroundColor('#efefef')
  28.   }
  29. }
复制代码
参考资料

官网文档
官网视频对应文档
router参考文档
官网视频
官网对应视频

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

羊蹓狼

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表