在鸿蒙开发中,UIAbility的跳转使用 router 方法.

在使用的时候需导入

  1. import router from '@ohos.router';

该方法接口成员如下:

1.interface RouterOptions

  1. interface RouterOptions {
  2. url: string; // 跳转页面的Url
  3. params?: Object; // 传给跳转页面的参数params
  4. }

该成员定义RouterOptions基本对象,在进行页面跳转时对应跳转的url和传入的参数params。

2.interface RouterState

  1. interface RouterState {
  2.  
  3. /**
  4. * Index of the current page in the stack.
  5. * NOTE: The index starts from 1 from the bottom to the top of the stack.
  6. * @since 8
  7. */
  8. index: number;
  9.  
  10. /**
  11. * Name of the current page, that is, the file name.
  12. * @since 8
  13. */
  14. name: string;
  15.  
  16. /**
  17. * Path of the current page.
  18. * @since 8
  19. */
  20. path: string;
  21. }

改成员定义RouterState基本对象,分别保存三个页面属性 index,name和path

index:记录当前页面在页面栈中的位置

name:记录当前页面的名称,也是文件名

path:记录当前页面的路径

3.interface EnableAlterOptions

  1. interface EnableAlertOptions {
  2.  
  3. /**
  4. * dialog context.
  5. * @since 8
  6. */
  7. message: string;
  8. }

该成员定义EnableAlertOptions对象,具有属性 message:string 保存日志文本

4.function push(options: RouterOptions): void

  1. /**
  2. * Navigates to a specified page in the application based on the page URL and parameters.
  3. * @param options Options.
  4. * @since 8
  5. */
  6. function push(options: RouterOptions):void;

该方法push接受类型为RouterOptions的参数,并进行页面的跳转和参数传递,返回void。

5.function replace(options: RouterOptions): void

  1. /**
  2. * Replaces the current page with another one in the application. The current page is destroyed after replacement.
  3. * @param options Options.
  4. * @since 8
  5. */
  6. function replace(options: RouterOptions):void;

该方法replace接受类型为RouterOptions的参数,进行页面的替换和参数传递,返回void。

类似的还有:

6.back()函数,返回上一个页面或者返回指定页面

  1. function back(options?: RouterOptions): void

7.clear()函数,清除所有历史页面,并且仅仅在栈顶保存当前页面

  1. /**
  2. * Clears all historical pages and retains only the current page at the top of the stack.
  3. * @since 8
  4. */
  5. function clear():void;

8.function getParams(): Object;

  1. /**
  2. * Obtains information about the current page params.
  3. * @returns Page params.
  4. * @since 8
  5. */
  6. function getParams(): Object;

该getParams方法用于获取页面缓存或者被传入的参数.

***方法使用实例***:

使用两个简单的页面跳转和返回来展示router方法的简单使用

两个页面:

./pages/index.ets

./pages/Second.ets

index.ets代码如下:

  1. import router from '@ohos.router';
  2.  
  3. @Entry
  4. @Component
  5. struct Index {
  6. @State message: string = 'Hello World'
  7.  
  8. build() {
  9. Row() {
  10. Text(this.message)
  11. Blank()
  12. Button('Next')
  13. .onClick(() => {
  14. router.push({
  15. url: 'pages/Second',
  16. params: {
  17. src: 'Index页面传来的数据',
  18. }
  19. })
  20. })
  21.  
  22. Column() {
  23. Text(this.message)
  24. .fontSize(50)
  25. .fontWeight(FontWeight.Bold)
  26. }
  27. .width('100%')
  28. }
  29. .height('100%')
  30. }
  31. }

Second.ets 代码如下:

  1. import router from '@ohos.router';
  2.  
  3. @Entry
  4. @Component
  5. struct Second {
  6. @State src: string = router.getParams()?.['src'];
  7.  
  8. build() {
  9. Row() {
  10. Column() {
  11. Text(this.src)
  12. .fontSize(50)
  13. .fontWeight(FontWeight.Bold)
  14.  
  15. Button('Back')
  16. .onClick(() => {
  17. router.back()
  18. })
  19. }
  20. .width('100%')
  21. }
  22. .height('100%')
  23. }
  24. }

鸿蒙开发学习笔记-UIAbility-Router页面跳转接口源码分析的更多相关文章

  1. Springboot学习04-默认错误页面加载机制源码分析

    Springboot学习04-默认错误页面加载机制源码分析 前沿 希望通过本文的学习,对错误页面的加载机制有这更神的理解 正文 1-Springboot错误页面展示 2-Springboot默认错误处 ...

  2. Java并发包源码学习之AQS框架(四)AbstractQueuedSynchronizer源码分析

    经过前面几篇文章的铺垫,今天我们终于要看看AQS的庐山真面目了,建议第一次看AbstractQueuedSynchronizer 类源码的朋友可以先看下我前面几篇文章: <Java并发包源码学习 ...

  3. Java并发编程笔记之Unsafe类和LockSupport类源码分析

    一.Unsafe类的源码分析 JDK的rt.jar包中的Unsafe类提供了硬件级别的原子操作,Unsafe里面的方法都是native方法,通过使用JNI的方式来访问本地C++实现库. rt.jar ...

  4. Spring笔记(5) - 声明式事务@EnableTransactionManagement注解源码分析

    一.背景 前面详解了实现Spring事务的两种方式的不同实现:编程式事务和声明式事务,对于配置都使用到了xml配置,今天介绍Spring事务的注解开发,例如下面例子: 配置类:注册数据源.JDBC模板 ...

  5. Dubbo入门到精通学习笔记(十九):MySQL源码编译安装、MySQL主从复制的配置

    文章目录 MySQL 源码编译安装(CentOS-6.6+MySQL-5.6) 一.服务器配置: 二.源码安装 MySQL5.6.26: MySQL主从复制的配置 环境 依赖课程 MySQL 主从复制 ...

  6. Django基于Pycharm开发之四[关于静态文件的使用,配置以及源码分析](原创)

    对于django静态文件的使用,如果开发过netcore程序的开发人员,可能会比较容易理解django关于静态文件访问的设计原理,个人觉得,这是一个middlerware的设计,但是在django中我 ...

  7. Linux学习笔记15—RPM包的安装OR源码包的安装

    RPM安装命令1. 安装一个rpm包rpm –ivh 包名“-i” : 安装的意思“-v” : 可视化“-h” : 显示安装进度另外在安装一个rpm包时常用的附带参数有:--force : 强制安装, ...

  8. Java并发包源码学习之线程池(一)ThreadPoolExecutor源码分析

    Java中使用线程池技术一般都是使用Executors这个工厂类,它提供了非常简单方法来创建各种类型的线程池: public static ExecutorService newFixedThread ...

  9. 转!!Java学习之自动装箱和自动拆箱源码分析

    自动装箱(boxing)和自动拆箱(unboxing)   首先了解下Java的四类八种基本数据类型   基本类型 占用空间(Byte) 表示范围 包装器类型 boolean 1/8 true|fal ...

  10. Java学习之自动装箱和自动拆箱源码分析

    自动装箱(boxing)和自动拆箱(unboxing) 首先了解下Java的四类八种基本数据类型   基本类型 占用空间(Byte) 表示范围 包装器类型 boolean 1/8 true|false ...

随机推荐

  1. Day15-static、抽象类、接口、内部类

    static.抽象类.接口.内部类 一.static关键字详解 1.静态的变量/方法 package Demo02; //static public class Student { private s ...

  2. opencv实战之透视变换

    import cv2 import numpy as np import pytesseract def cv_show(imgname,img): cv2.imshow(imgname,img) c ...

  3. Educational Codeforces Round 2 个人总结A-E

    Educational Codeforces Round 2 A. Extract Numbers 简单的模拟 bool check(string op) { if(op.size()==1& ...

  4. RabbitMq的部署(docker)和操作(python)详解

    一.简介: RabbitMq 是实现了高级消息队列协议(AMQP)的开源消息代理中间件.消息队列是一种应用程序对应用程序的通行方式,应用程序通过写消息,将消息传递于队列,由另一应用程序读取 完成通信. ...

  5. 3html5

    <label>网址:</label><input type="url" name="" required><br> ...

  6. Delaunay triangulation 的实现

    在GitHub 找到的别人的代码:https://github.com/earthwjl/DelaunayTriangulate 解压后是这样的:(没有x64) 直接就有了.sln工程文件,于是用Vi ...

  7. SDC细节归纳

    能否写出一份严谨的SDC约束文件,决定了芯片tapeout后数字电路能否正常工作,或者少一些bug.所以写好SDC约束文件,是芯片设计的关键一步. 因此,归纳.整理SDC约束的细节要点很重要,有助于减 ...

  8. C# 将实体转xml/xml转实体

    xml转实体 /// <summary> /// 把xml转换成实体 /// </summary> /// <typeparam name="T"&g ...

  9. git 本地项目初始化提交至仓库

    命令行指令 Git初始化配置 git config --global user.name"abc" git config --global user.email"1234 ...

  10. excel编辑受限的密码保护破解

    录制一个宏并且执行: Public Sub 工作表保护密码() Const DBLSPACE As String = vbNewLine & vbNewLine Const AUTHORS A ...