学习ArkTS --页面路由

页面路由 router

页面路由是指在应用程序中实现不同页面之间的跳转和数据传递。

页面栈的最大容量上限为32个页面,使用router.clear()方法可以清空页面栈,释放内存。

Router有两种页面跳转模式,分别是:

  1. router.pushUrl():目标页不会替换当前页面,而是压入页面栈,因此可以用router.back()返回当前页
  2. router.replaceUrl(): 目标替换当前页面,当前页会被销毁并释放资源,无法返回当前页。

Router有两种页面实例模式,分别是:

  1. standard: 标准实例模式,每次跳转都会新建一个目标页并压入栈顶。默认就是这种模式。
  2. Single: 单实例模式,如果目标页面已经在栈中,则离栈顶最近的同Url页面会被移动到栈顶并重新加载
复制代码
import router from '@ohos.router';
class RouterInfo{
  url: string
  title: string
  constructor(url:string, title: string)
  {
    this.url = url;
    this.title = title;
  }
}

@Entry
@Component
struct Index {

  @State message: string = '页面列表'
  private routers: RouterInfo[] = [
    new RouterInfo('pages/Imagepage','图片查看案例'),
    new RouterInfo('pages/ItemPages','商品烈表案例'),
    new RouterInfo('pages/StatePage','jack案例'),
    new RouterInfo('pages/ProPage','任务列表案例')
]
  build(){
    Column() {
      Text(this.message)
        .fontSize(50)
        .fontWeight(FontWeight.Bold)
        .height(80)
        .onClick(() =>{
          router.back() // 返回上一页
        })

      List({space: 15}){
        ForEach(this.routers,
          (router, index) => {
            ListItem(){
              this.RouterItem(router, index+1)
            }
          })
      }
      .layoutWeight(1)
      .alignListItem(ListItemAlign.Center)
      .width('100%')
    }
    .width('100%')
    .height('100%')
  }
  @Builder
  RouterItem(r:RouterInfo, i: number){
    Row(){
      Text(i + '.')
        .fontSize(20)
        .fontColor(Color.White)
      Blank()
      Text(r.title)
        .fontColor(Color.White)
        .fontSize(20)
    }
    .width('90%')
    .padding(12)
    .backgroundColor('#38f')
    .borderRadius(20)
    .shadow({radius: 6, color: '#4F000000', offsetX: 2, offsetY: 4})
    .onClick(() => {
      router.pushUrl(
        {
          url:r.url,
          params: {id: i}
        },
        router.RouterMode.Single,
        err => {
          if (err){
            console.log(`路由失败, errorCode: ${err.code} errMsg:${err.message}`);
          }
        }
      )
    })
  }
}


相关推荐
虾球xz44 分钟前
游戏引擎学习第276天:调整身体动画
c++·学习·游戏引擎
虾球xz1 小时前
游戏引擎学习第275天:将旋转和剪切传递给渲染器
c++·学习·游戏引擎
qq_386322692 小时前
华为网路设备学习-21 IGP路由专题-路由过滤(filter-policy)
前端·网络·学习
J先生x2 小时前
【IP101】图像处理进阶:从直方图均衡化到伽马变换,全面掌握图像增强技术
图像处理·人工智能·学习·算法·计算机视觉
虾球xz6 小时前
游戏引擎学习第268天:合并调试链表与分组
c++·学习·链表·游戏引擎
Y3174296 小时前
Python Day23 学习
python·学习
song_ly0017 小时前
深入理解软件测试覆盖率:从概念到实践
笔记·学习·测试
DIY机器人工房7 小时前
[6-2] 定时器定时中断&定时器外部时钟 江协科技学习笔记(41个知识点)
笔记·stm32·单片机·学习·江协科技
海尔辛8 小时前
学习黑客5 分钟小白弄懂Windows Desktop GUI
windows·学习
烟雨迷10 小时前
Linux环境基础开发工具的使用(yum、vim、gcc、g++、gdb、make/Makefile)
linux·服务器·学习·编辑器·vim