鴻蒙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.pswp.cn/web/43999.shtml
繁體地址,請注明出處:http://hk.pswp.cn/web/43999.shtml
英文地址,請注明出處:http://en.pswp.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 解題思路 該問…