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


相关推荐
comyccccc2 小时前
STM32F407VET6核心板的部分电路绘制学习笔记
笔记·stm32·学习
叼烟扛炮2 小时前
Linux 学习笔记(三十六):并发进阶——读写锁 vs 自旋锁,到底怎么选?
linux·运维·服务器·笔记·学习
是隼人2 小时前
buuctf-pwn [极客大挑战 2019]Not Bad(orw+jmp)题解(学习过程持续更新)
学习·安全·pwn入门·ctf入门
lazy H3 小时前
【计算机网络】第 12 篇:IPv6 详解——地址格式、地址类型与双栈网络
网络·经验分享·后端·学习·计算机网络·408
(●—●)橘子……3 小时前
力扣 第525场周赛 练习理解
数据结构·python·学习·算法·leetcode
一只QAQ3 小时前
python语法学习
linux·python·学习
東隅已逝,桑榆非晚3 小时前
map和set的使用
数据结构·c++·笔记·学习
dadaobusi3 小时前
error:field ifru_addr has incomplete type sockaddr
学习
Z21-X4 小时前
云原生运维笔记:硬核拆解OpenStack计算与存储——从主机聚合到Cinder挂载全流程
运维·笔记·学习·云原生·openstack
坤坤子吖4 小时前
Linux网络编程基础:从 MAC 到 IP,理解网络数据是怎么传输的
linux·网络·笔记·学习