鸿蒙Navigation的页面跳转官方代码

星河版本

文章部分代码来源于官方

文章部分代码来源于官方只是自己改了容易理解

与API4不同的Navigation

新版本使用的思路是

1、创建页面栈

pageInfos: NavPathStack = new NavPathStack();

2、resources/base/profile创建 router_map.json 文件

{"routerMap": [{"name": "pageOne","pageSourceFile": "src/main/ets/pages/Navigation/EntryPageOne.ets","buildFunction": "PageOneBuilder","data": {"description": "this is pageOne"}},{"name": "pageTwo","pageSourceFile": "src/main/ets/pages/Navigation/EntryPageTwo.ets","buildFunction": "PageTwoBuilder","data": {"description": "this is pageTwo"}}]
}

3、将配置文件载入module.json5

{"module": {"name": "entry","type": "entry","description": "$string:module_desc","mainElement": "EntryAbility","deviceTypes": ["phone","tablet","2in1"],"deliveryWithInstall": true,"installationFree": false,"pages": "$profile:main_pages","routerMap": "$profile:router_map","abilities": [{"name": "EntryAbility","srcEntry": "./ets/entryability/EntryAbility.ets","description": "$string:EntryAbility_desc","icon": "$media:layered_image","label": "$string:EntryAbility_label","startWindowIcon": "$media:startIcon","startWindowBackground": "$color:start_window_background","exported": true,"skills": [{"entities": ["entity.system.home"],"actions": ["action.system.home"]}]}],"extensionAbilities": [{"name": "EntryBackupAbility","srcEntry": "./ets/entrybackupability/EntryBackupAbility.ets","type": "backup","exported": false,"metadata": [{"name": "ohos.extension.backup","resource": "$profile:backup_config"}],}]}
}

加入 "routerMap": "$profile:router_map"

$的用法之一读取本地resources/base下目录,另一个用于响应式变量

4、创建页面

NavigationExample.ets

/*
* Copyright (C) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/@Entry
@Component
@Preview
struct NavigationExample {pageInfos: NavPathStack = new NavPathStack();build() {Navigation(this.pageInfos) {Column({ space: 12 }) {Text("首页").fontSize("15").width("100%").height('20vp').fontColor(Color.Black)Button("芜湖芜湖")       .width("100%").height('20vp')Button('第一个页面', { stateEffect: true, type: ButtonType.Capsule }).width("100%").height('20vp').onClick(() => {//Push the NavDestination page information specified by name onto the stack, and pass the data as param.this.pageInfos.pushPathByName('pageOne', null);})Button('第二个页面', { stateEffect: true, type: ButtonType.Capsule }).width("100%").height('20vp').onClick(() => {//Push the NavDestination page information specified by name onto the stack, and pass the data as param.this.pageInfos.pushPathByName('pageTwo', null);})}.width($r('app.string.navDestination_width')).height($r('app.string.navDestination_height')).justifyContent(FlexAlign.End).padding({bottom: $r('app.string.column_padding'),left: $r('app.string.column_padding'),right: $r('app.string.column_padding')})}.title($r('app.string.entry_index_title'))}
}

EntryPageOne.ets

/*
* Copyright (C) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/import Logger from '../../common/utils/Logger';@Builder
export function PageOneBuilder(name: string, param: Object) {PageOne()
}const COLUMN_SPACE: number = 12;@Component
export struct PageOne {pageInfos: NavPathStack = new NavPathStack();build() {NavDestination() {Column({ space: 10 }) {Text("第一个页面").fontSize("15").width("100%").height('20vp').fontColor(Color.Black)Button('返回首页', { stateEffect: true, type: ButtonType.Capsule }).width("100%").height('20vp').onClick(() => {//Clear all pages in the stack.this.pageInfos.clear();})Button('第二个页面', { stateEffect: true, type: ButtonType.Capsule }).width("100%").height('20vp').onClick(() => {//Push the NavDestination page information specified by name onto the stack, and pass the data as param.this.pageInfos.pushPathByName('pageTwo', null);})Button("第一个页面", { stateEffect: true, type: ButtonType.Capsule }).width("100%").height('20vp').onClick(() => {//Push the NavDestination page information specified by name onto the stack, and pass the data as param.this.pageInfos.pushPathByName('pageOne', null);})}.width($r('app.string.navDestination_width')).height($r('app.string.navDestination_height')).justifyContent(FlexAlign.End).padding({bottom: $r('app.string.column_padding'),left: $r('app.string.column_padding'),right: $r('app.string.column_padding')})}.title('entry-pageOne').onReady((context: NavDestinationContext) => {this.pageInfos = context.pathStack;Logger.info("current page config info is " + JSON.stringify(context.getConfigInRouteMap()));})}
}

EntryPageTwo.ets

/*
* Copyright (C) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/import Logger from '../../common/utils/Logger';@Builder
export function PageTwoBuilder(name: string, param: Object) {PageTwo()
}const COLUMN_SPACE: number = 12;@Component
export struct PageTwo {pageInfos: NavPathStack = new NavPathStack();build() {NavDestination() {Column({ space: 10 }) {Text("第2个页面").fontSize("15").width("100%").height('20vp').fontColor(Color.Black)Button("返回首页", { stateEffect: true, type: ButtonType.Capsule }).width("100%").height('20vp').onClick(() => {this.pageInfos.clear(); //Clear all pages in the stack})Button("第一个页面", { stateEffect: true, type: ButtonType.Capsule }).width("100%").height('20vp').onClick(() => {//Push the NavDestination page information specified by name onto the stack, and pass the data as param.this.pageInfos.pushPathByName('pageOne', null);})Button("第二个页面", { stateEffect: true, type: ButtonType.Capsule }).width("100%").height('20vp').onClick(() => {//Push the NavDestination page information specified by name onto the stack, and pass the data as param.this.pageInfos.pushPathByName('pageTwo', null);})}.width($r('app.string.navDestination_width')).height($r('app.string.navDestination_height')).justifyContent(FlexAlign.End).padding({bottom: $r('app.string.column_padding'),left: $r('app.string.column_padding'),right: $r('app.string.column_padding')})}.title('entry-pageTwo').onReady((context: NavDestinationContext) => {this.pageInfos = context.pathStackLogger.info('current page config info is ' + JSON.stringify(context.getConfigInRouteMap()));})}
}

string.json

{"string": [{"name": "module_desc","value": "module description"},{"name": "EntryAbility_desc","value": "description"},{"name": "EntryAbility_label","value": "Navigation systemRouting"},{"name": "navDestination_width","value": "100%"},{"name": "navDestination_height","value": "100%"},{"name": "button_width","value": "100%"},{"name": "button_height","value": "40"},{"name": "column_padding","value": "16"},{"name": "entry_index_title","value": "NavIndex"},{"name": "entry_index","value": "toIndex"},{"name": "entry_pageOne","value": "toEntryPageOne"},{"name": "entry_pageTwo","value": "toEntryPageTwo"},{"name": "harA_pageOne","value": "toHarAPageOne"},{"name": "harA_pageTwo","value": "toHarAPageTwo"},{"name": "harB_pageOne","value": "toHarBPageOne"},{"name": "harB_pageTwo","value": "toHarBPageTwo"},{"name": "hspA_pageOne","value": "toHspAPageOne"},{"name": "hspA_pageTwo","value": "toHspAPageTwo"},{"name": "hspB_pageOne","value": "toHspBPageOne"},{"name": "hspB_pageTwo","value": "toHspBPageTwo"}]
}

5、修改 EntryAbility.ets

windowStage.loadContent('pages/Navigation/NavigationExample', (err) => {if (err.code) {hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? '');return;}hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.');});
}

这里换成自己的就好了pages/Navigation/NavigationExample

演示

screenshots

注意

按照官方的规范是需要创建router_map.json并且将配置文件负载到module.json5里面的,每一个组建页面都需要实现一个

@Builder
export function PageOneBuilder(name: string, param: Object) {PageOne()
}

创建自己的函数在router_map.json中配置buildFunction

{"name": "pageOne","pageSourceFile": "src/main/ets/pages/Navigation/EntryPageOne.ets","buildFunction": "PageOneBuilder","data": {"description": "this is pageOne"}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/web/43999.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

数电设计提问求帮助,出租车计费器。

🏆本文收录于《CSDN问答解惑-》专栏,主要记录项目实战过程中的Bug之前因后果及提供真实有效的解决方案,希望能够助你一臂之力,帮你早日登顶实现财富自由🚀;同时,欢迎大家关注&&收藏&…

Autosar诊断实战系列28-2E写DID Pending期间偶发回NRC0x13问题排查

本文框架 前言1.问题描述2.问题复现3.问题分析问题1:为何在2E Pending期间会发送功能寻址的10 01回NRC13?问题2:在ECU Pending期间收到功能寻址10 01,MCU需要如何处理?问题3:DcmDslConnection是如何定义的?问题4:功能寻址于物理寻址是否对应不同的DcmDslConnection?问…

Pandas数据可视化宝典:解锁图形绘制与样式自定义的奥秘

Pandas数据可视化宝典:解锁图形绘制与样式自定义的奥秘 引言 数据可视化是将数据以图形或图像的形式展示出来,使复杂的数据更容易被人类理解和分析。在数据分析、商业智能、科学研究等领域,数据可视化都扮演着至关重要的角色。Pandas作为一…

如何通过 Java 来完成 zip 文件与 rar 文件的解压缩?

目录 一、用到的知识点 二、代码展示(分解版) 三、代码展示(整体版) 一、用到的知识点 1.IO流: Input:输入,通过“输入流”进行文件的读取操作 Output:输出,通过“输出流”进行文件的写入操作 2.文件操作相关: File类&#xff…

Point Cloud Library (PCL) for Python - pclpy 安装指南 (2)

Point Cloud Library (PCL) for Python - pclpy 安装指南 (1) 导入库 from pclpy import pcl import numpy as np导入pclpy库中的pcl模块,用于处理点云数据。numpy库用于处理数值数据。 读取点云 cloud pcl.PointCloud.PointXYZRGB() pcl.io.loadPCDFile(F:\\bunn…

2024年西安铁一中集训DAY1---- 杂题选讲

文章目录 牛客练习赛125 E 联谊活动(枚举,分讨)牛客练习赛125 F 玻璃弹珠(类莫队,离线询问,数据结构)2024ccpc长春邀请赛 D Parallel Lines(随机化)2024ccpc长春邀请赛 E…

STM32智能健康监测系统教程

目录 引言环境准备智能健康监测系统基础代码实现:实现智能健康监测系统 4.1 数据采集模块 4.2 数据处理与分析模块 4.3 通信与网络系统实现 4.4 用户界面与数据可视化应用场景:健康监测与优化问题解决方案与优化收尾与总结 1. 引言 智能健康监测系统通…

k8s 容器环境下的镜像如何转换为docker 使用

在无法连接registry 的环境中,想要把 crictl 中的镜像给docker 使用,应该怎么处理? 其实容器镜像是通用的,crictl 和ctr 以及docker 镜像是可以互相使用的,因为docker 在1.10版本之后遵从了OCI。所以crictl 环境下的镜…

Android Studio 的Gradle下载慢,Gradle切换下载源

看图 下面的文字地址因为转义符号的问题,https后面少了一个斜杠看图片进行补充,直接复制不知道能不能用 distributionUrlhttps://mirrors.cloud.tencent.com/gradle/gradle-8.7-bin.zip

浪潮服务器内存物理插槽位置

浪潮服务器内存物理插槽位置 如下图所示

Doze和AppStandby白名单配置方法和说明

机制 配置路径 配置案例 说明 影响机制 调试命令 Doze /platform/frameworks/base /data/etc/platform.xml allow-in-power-save 【系统应用Doze白名单配置】 Doze\Job\AppStandby\Alarm\WakeLock\Sync 查看Doze白名单:adb shell dumpsys deviceidle 添加Doze白名单…

漏洞挖掘之信息搜集(一)

本篇文章只从信息搜集的步骤整理 一、选好你要挖掘的src 这一点一定要明确,定好一个,然后下定决心一定要挖到一个高危 常见src总结: 360众测(需要考核) 漏洞盒子(还可以,审核很慢)----基本无要求 补天:有钱,但要求高,百度收录占比权重大于等于1或者或者谷歌权…

前端进阶全栈计划:Java基础语法

前言 本教程旨在帮助初学者系统地掌握Java的基础知识。我们将从Java的基本语法开始,逐步深入到面向对象编程、异常处理、多线程编程等核心概念。无论你是编程新手,还是希望夯实基础的开发者,这份指南都将带你走进Java的世界,打下坚…

昇思MindSpore学习笔记6-06计算机视觉--Vision Transormer图像分类

摘要: 记录MindSpore AI框架使用ViT模型在ImageNet图像数据分类上进行训练、验证、推理的过程和方法。包括环境准备、下载数据集、数据集加载、模型解析与构建、模型训练与推理等。 一、概念 1. ViT模型 Vision Transformer 自注意结构模型 Self-Attention Tran…

MySQL(基础篇)

DDL (Data Definition Language) 数据定义语言,用来定义数据库对象(数据库,表, 字段) DML (Data Manipulation Languag) 数据操作语言,用来对数据库表中的数据进行增删改 DQL (Data Query Language) 数据查询语言,用…

前缀,中缀,后缀表达式

前缀表达式 前缀表达式(也称为波兰式)是一种将运算符放在操作数之前的表示数学表达式的方法。在前缀表达式中,操作符出现在它们所操作的操作数之前。 例如,将中缀表达式5 3转换为前缀表达式,可以写成 5 3。在这个例…

9 个让 Python 性能更高的小技巧,你掌握了吗?

我们经常听到 “Python 太慢了”,“Python 性能不行”这样的观点。但是,只要掌握一些编程技巧,就能大幅提升 Python 的运行速度。 今天就让我们一起来看下让 Python 性能更高的 9 个小技巧 python学习资料分享(无偿)…

数据(图像)增广

一、数据增强 1、增加一个已有数据集,使得有更多的多样性,比如加入不同的背景噪音、改变图片的颜色和形状。 2、增强数据是在线生成的 3、增强类型: (1)翻转 (2)切割 (3&#xf…

金龙鱼:只是躺枪?

中储粮罐车运输油罐混用事件持续发酵,食用油板块集体躺枪。 消费者愤怒的火,怕是会让食用油企们一点就着。 今天,我们聊聊“油”茅——金龙鱼。 一边是业内人士指出,油罐混用的现象普遍存在,另一边是金龙鱼回应称&am…

2972.力扣每日一题7/11 Java(击败100%)

博客主页:音符犹如代码系列专栏:算法练习关注博主,后期持续更新系列文章如果有错误感谢请大家批评指出,及时修改感谢大家点赞👍收藏⭐评论✍ 目录 解题思路 解题方法 时间复杂度 空间复杂度 Code 解题思路 该问…