一、路由导航

路由导航是指在应用程序中通过路径导航定位到特定页面的过程。路由导航的实现通常采用路由器(router)来进行管理,路由器根据路径的不同值将用户请求导向到不同的页面。

在HarmonyOS中路由导航主要有:页面跳转、页面返回和页面返回前增加一个询问框

1.编程路由

1.1 页面跳转

页面跳转相关作用:

Router模块提供了两种跳转模式: router.pushUrl() 和 router.replaceUrl()。router.pushUrl() 可以通过返回键或者调用router.back()方法返回到当前页:

Router模块提供了两种实例模式: Standard 和 Single:

️1.1.1 保留主页在页面栈中,以便返回时恢复状态

主页(Home)和 详情页(Detail)

1、主页

import router from '@ohos.router';
// 在Home页面中
function onJumpClick(): void {
router.pushUrl({
url: 'pages/ImagePage' // 目标url
}, router.RouterMode.Standard, (err) => {
if (err) {
console.error(`Invoke pushUrl failed, code is ${err.code}, message is ${err.message}`);
return;
}
console.info('Invoke pushUrl succeeded.');
});
}
@Entry
@Component
struct Index {
build() {
Row() {
Button('跳转到图片页面')
.onClick(e=>{
onJumpClick()
})
}.alignItems(VerticalAlign.Center).justifyContent(FlexAlign.Center).backgroundColor(0xffd306).height('100%').width('100%')
}
}

2、详情页

import router from '@ohos.router';
@Entry //FA模式必须有这个
@Component
struct Index {
@State imageWidth: number = 150 build() {
Column() {
Row(){
Image($r('app.media.icon'))
.width(this.imageWidth)
}
.width('100%')
.height(400)
.justifyContent(FlexAlign.Center) Row(){
Text($r('app.string.width_label'))
.fontSize(20)
.fontWeight(FontWeight.Bold) TextInput({text: this.imageWidth.toFixed(0)})
.width(150)
.backgroundColor('#FFF')
.type(InputType.Number)
.onChange( value => {
this.imageWidth = parseInt(value)
})
}
.width('100%')
.padding({left: 14, right: 14})
.justifyContent(FlexAlign.SpaceBetween) Divider()
.width('91%') Row(){
Button('缩小')
.width(80)
.fontSize(20)
.onClick(() => {
if(this.imageWidth >= 10){
this.imageWidth -= 10
}
}) Button('放大')
.width(80)
.fontSize(20)
.onClick(() => {
if(this.imageWidth < 300){
this.imageWidth += 10
}
}) }
.width('100%')
.margin({ top: 35, bottom: 35 })
.justifyContent(FlexAlign.SpaceEvenly)
Button('回到首页')
.width(160)
.fontSize(20)
.onClick(() => {
router.back()
}) Slider({
min: 100,
max: 300,
value: this.imageWidth,
step: 10,
})
.width('100%')
.blockColor('#36D')
.trackThickness(5)
.showTips(true)
.onChange(value => {
this.imageWidth = value
})
}
.width('100%')
.height('100%')
}
}

️1.1.2 不保留主页在页面栈中,在返回时直接退出应用

登录页(Login)和 个人中心页(Profile)的切换适用案例

1、登录页

function onJumpClick(): void {
router.replaceUrl({
url: 'pages/ImagePage' // 目标url
}, router.RouterMode.Standard, (err) => {
if (err) {
console.error(`Invoke replaceUrl failed, code is ${err.code}, message is ${err.message}`);
return;
}
console.info('Invoke replaceUrl succeeded.');
})
}
️1.1.3 保留主页在页面栈中,以便返回时恢复状态

设置页(Setting)和一个主题切换页

1、设置页

// 在Setting页面中
function onJumpClick(): void {
router.pushUrl({
url: 'pages/Theme' // 目标url
}, router.RouterMode.Single, (err) => {
if (err) {
console.error(`Invoke pushUrl failed, code is ${err.code}, message is ${err.message}`);
return;
}
console.info('Invoke pushUrl succeeded.');
});
}
️1.1.4 保留主页在页面栈中,以便返回时恢复状态

搜索结果列表页(SearchResult)和一个搜索结果详情页(SearchDetail)

1、搜索结果列表页

// 在SearchResult页面中
function onJumpClick(): void {
router.replaceUrl({
url: 'pages/SearchDetail' // 目标url
}, router.RouterMode.Single, (err) => {
if (err) {
console.error(`Invoke replaceUrl failed, code is ${err.code}, message is ${err.message}`);
return;
}
console.info('Invoke replaceUrl succeeded.');})
}

1.2 页面参数

️1.2.1 主页页面参数传递和获取

1、参数传递

class DataModelInfo {
age: number;
} class DataModel {
id: number;
info: DataModelInfo;
} function onJumpClick(): void {
// 在Home页面中
let paramsInfo: DataModel = {
id: 123,
info: {
age: 20
}
}; router.pushUrl({
url: 'pages/Detail', // 目标url
params: paramsInfo // 添加params属性,传递自定义参数
}, (err) => {
if (err) {
console.error(`Invoke pushUrl failed, code is ${err.code}, message is ${err.message}`);
return;
}
console.info('Invoke pushUrl succeeded.');
})
}

2、参数获取

const params = router.getParams(); // 获取传递过来的参数对象
const id = params['id']; // 获取id属性的值
const age = params['info'].age; // 获取age属性的值
️1.2.1 返回页页面参数传递和获取

1、参数传递

router.back({
url: 'pages/Home',
params: {
info: '来自Home页'
}
});

2、参数获取

onPageShow() {
const params = router.getParams(); // 获取传递过来的参数对象
const info = params['info']; // 获取info属性的值
}

1.3 页面返回前增加一个询问框

️1.3.1 默认询问框
import router from '@ohos.router';

function onJumpClick(): void {
router.pushUrl({
url: 'pages/ImagePage' // 目标url
}, router.RouterMode.Standard, (err) => {
if (err) {
console.error(`Invoke replaceUrl failed, code is ${err.code}, message is ${err.message}`);
return;
}
console.info('Invoke replaceUrl succeeded.');
})
}
@Entry
@Component
struct Index {
build() {
Row() {
Button('跳转到图片页面')
.onClick(e=>{
onJumpClick()
})
}.alignItems(VerticalAlign.Center).justifyContent(FlexAlign.Center).backgroundColor(0xffd306).height('100%').width('100%')
}
}

️1.3.2 自定义询问框
import router from '@ohos.router';
import promptAction from '@ohos.promptAction'; function onBackClick() {
// 弹出自定义的询问框
promptAction.showDialog({
message: '您还没有完成支付,确定要返回吗?',
buttons: [
{
text: '取消',
color: '#FF0000'
},
{
text: '确认',
color: '#0099FF'
}
]
}).then((result) => {
if (result.index === 0) {
// 用户点击了“取消”按钮
console.info('User canceled the operation.');
} else if (result.index === 1) {
// 用户点击了“确认”按钮
console.info('User confirmed the operation.');
// 调用router.back()方法,返回上一个页面
router.back();
}
}).catch((err) => {
console.error(`Invoke showDialog failed, code is ${err.code}, message is ${err.message}`);
})
} @Entry
@Component
struct Index {
@State imageWidth: number = 150 build() {
Column() {
Row(){
Image($r('app.media.icon'))
.width(this.imageWidth)
}
.width('100%')
.height(400)
.justifyContent(FlexAlign.Center) Row(){
Text($r('app.string.width_label'))
.fontSize(20)
.fontWeight(FontWeight.Bold) TextInput({text: this.imageWidth.toFixed(0)})
.width(150)
.backgroundColor('#FFF')
.type(InputType.Number)
.onChange( value => {
this.imageWidth = parseInt(value)
})
}
.width('100%')
.padding({left: 14, right: 14})
.justifyContent(FlexAlign.SpaceBetween) Divider()
.width('91%') Row(){
Button('缩小')
.width(80)
.fontSize(20)
.onClick(() => {
if(this.imageWidth >= 10){
this.imageWidth -= 10
}
}) Button('放大')
.width(80)
.fontSize(20)
.onClick(() => {
if(this.imageWidth < 300){
this.imageWidth += 10
}
}) }
.width('100%')
.margin({ top: 35, bottom: 35 })
.justifyContent(FlexAlign.SpaceEvenly)
Button('回到首页')
.width(160)
.fontSize(20)
.onClick(() => {
onBackClick()
}) Slider({
min: 100,
max: 300,
value: this.imageWidth,
step: 10,
})
.width('100%')
.blockColor('#36D')
.trackThickness(5)
.showTips(true)
.onChange(value => {
this.imageWidth = value
})
}
.width('100%')
.height('100%')
}
}

写在最后

  • 如果你觉得这篇内容对你还蛮有帮助,我想邀请你帮我三个小忙:
  • 点赞,转发,有你们的 『点赞和评论』,才是我创造的动力。
  • 关注小编,同时可以期待后续文章ing,不定期分享原创知识。
  • 更多鸿蒙最新技术知识点,请关注作者博客:https://t.doruo.cn/14DjR1rEY

鸿蒙HarmonyOS实战-ArkUI组件(页面路由)的更多相关文章

  1. 从微信小程序到鸿蒙js开发【11】——页面路由

    目录: 1.router.push()&wx.navigateTo() 2.router.replace()&wx.redirectTo() 3.router.back()&w ...

  2. Android组件化开发-----页面路由(ARouter)

    平时开发中,我们经常用到页面跳转功能.之前我一直使用Intent过跳转 Intent intent = new Intent(A.this, B.class); intent.putExtra(&qu ...

  3. ArkUI 页面路由

    很多应用由多个页面组成,不同的页面承担着不一样的功能.比如,从音乐列表页面点击歌曲,跳转到该歌曲的播放界面.开发者需要通过页面路由将这些页面串联起来. 在 js -> default -> ...

  4. 鸿蒙的远程交互组件应用及微信小程序的远程交互组件应用

    注:鸿蒙的远程交互组件应用相对复杂 ,访问网络时,首先要配置网络权限,华为官方文档有问题,在此引用我老师配置的模板,见附件 过程:1.导入鸿蒙的网络请求模块fetch 2.发起对服务器的请求(在这过程 ...

  5. 微信小程序之页面路由(九)

    [未经允许,请勿以任何形式转载] 什么是路由? 我们通常理解的路由指分组数据包从源到目的地时,决定端到端路径的网络范围的进程: 借用上面的定义,我们可以理解小程序页面路由,根据路由规则(路径)从一个页 ...

  6. 微信小程序之页面路由

    路由方式 简介 对于路由的触发方式以及页面生命周期函数如下: 路由方式 触发时机 路由前页面 路由后页面 初始化 小程序打开的第一个页面   onLoad, onSHow 打开新页面 调用 API w ...

  7. iOS 组件化 —— 路由设计思路分析

    原文 前言 随着用户的需求越来越多,对App的用户体验也变的要求越来越高.为了更好的应对各种需求,开发人员从软件工程的角度,将App架构由原来简单的MVC变成MVVM,VIPER等复杂架构.更换适合业 ...

  8. Vue小项目二手书商城:(一)准备工作、组件和路由

    本项目基于vue2.5.2,与低版本部分不同之处会在(五)参考资料中提出 完整程序:https://github.com/M-M-Monica/bukesi 实现内容: 资源准备(mock数据) 组件 ...

  9. vue-router之路由钩子(组件内路由钩子必须在路由组件调用,子组件没用)

    模式 vue-router中的模式选项主要在router实例化的时候进行定义的,如下 const router = new VueRouter({ mode: 'history', // 两种类型hi ...

  10. 054——VUE中vue-router之实例讲解定义一下单页面路由

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

随机推荐

  1. Java //数组的反转

    1 //数组的反转 2 //方式一 3 System.out.println("数组的反转"); 4 5 // for(int i = 0; i <arr.length/2; ...

  2. Codeforces Round 928 (Div. 4)(A、B、C、D、E、G)

    目录 A B C D E G A 统计A.B输出 #include <bits/stdc++.h> #define int long long #define rep(i,a,b) for ...

  3. Educational Codeforces Round 135 (Rated for Div. 2)C. Digital Logarithm(思维)

    目录 题目链接 题意 题解 代码 题目链接 C. Digital Logarithm 题意 给两个长度位\(n\)的数组\(a\).\(b\),一个操作\(f\) 定义操作\(f\)为,\(a[i]= ...

  4. XAF新手入门 - 应用程序模型(Application Model)

    应用程序模型不仅是XAF的核心,它更是XAF的最大特色,它自动收集XAF项目中的信息,用于生成不同平台的UI.由于应用程序模型在XAF中的重要性,官方文档对它的介绍比较详细,大家可以直接阅读官方文档 ...

  5. USB数据传输与手机授权:充电宝常规使用不需要授权

    概述 此篇为解答充电宝骗局问题,骗局概述:两个人做局,以充测试充电宝是否损坏为由,插到受骗者手机上,受骗者允许了手机弹出的授权请求后,偷偷将病毒注入手机. Q:什么情况下手机会弹出授权? A:手机用数 ...

  6. vscode 自定义 当前行转大写快捷键 alt + shift + U

    vscode 自定义 当前行转大写快捷键 alt + shift + U

  7. InputRegZen.vue 正则Input 限制输入框输入内容

    核心内容 已经 perfect,没有用外库,原生完成 用的 iview的Input组件 封装 // InputRegZen.vue <template> <div> <I ...

  8. Windows10 Linux 子系统的骚操作之 Ctrl+Alt+T shell启动终端

    巨硬的 WSL(Windows Subsystem for Linux)是真的好用 但是,由于本人比较喜欢按快捷键.所以一直想着找到Windows上安装的ubuntu.exe文件,搞个快捷方式,然后再 ...

  9. ubuntu下安装numpy和scipy正确方法

    1.numpy NumPy(Numeric Python)是用Python进行科学计算的基本软件包. NumPy是Python编程语言的扩展,增加了对大型多维数组和矩阵的支持,以及一个大型的高级数学函 ...

  10. 用免费GPU部署自己的stable-diffusion-学习笔记

    最近由于工作需要,开始学习AI+大模型,零基础,听从同事的推荐报名参加了一个免费学习团队,本文是整理的一些学习笔记. 课程是趋动云提供支持的,在注册时赠送了足够学习使用的188算力.项目在趋动云上可以 ...