学习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}`);
          }
        }
      )
    })
  }
}


相关推荐
yiangL_13 小时前
Python 学习笔记:递归、turtle绘图、字符串与函数
笔记·python·学习
优化Henry16 小时前
LTE 站点 S1 链路反复闪断导致小区频闪退服故障排查
运维·网络·学习·信息与通信·tdd
β添砖java20 小时前
Transformer大白话学习笔记
笔记·学习·transformer
kevin_org1 天前
LangChain入门学习(四)
python·学习·langchain·fastapi
瑞克-桑切斯2 天前
Kubernetes 1.28 保姆级新手学习教程
运维·学习·docker·云原生·容器·kubernetes
kaixin_啊啊2 天前
【三种鲑科鱼类比较基因组学分析鉴定出参与细胞内寄生菌鲑立克次体抗性的功能候选基因】论文学习
学习
CappuccinoRose2 天前
Redis 学习文档
redis·后端·学习·缓存·session·非关系型数据库·gossip
qingwufeiyang_5302 天前
Docker学习笔记2
笔记·学习
Z21-X2 天前
云原生运维学习笔记:从操作系统底层到 Kubernetes 集群的深度实践
运维·笔记·学习·云原生·kubernetes
Century_Dragon2 天前
把高压回路“看透” 新能源汽车动力系统拆装与检测实训MR软件
学习