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

news2024/9/20 5:47:43

星河版本

文章部分代码来源于官方

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

与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.pathStack
      Logger.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.coloradmin.cn/o/1917983.html

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

相关文章

视频监控汇聚平台LntonCVS视频集中存储平台解决负载均衡的方案

随着技术的进步和企业对监控需求的增加,视频监控系统规模不断扩大,接入大量设备已成常态化挑战。为应对这一挑战,视频汇聚系统LntonCVS视频融合平台凭借其卓越的高并发处理能力,为企业视频监控管理系统提供可靠的负载均衡服务保障…

公司网站建站模板源码系统 响应式网站模版 随心自定义 带完整的代码包以及搭建部署教程

系统概述 公司网站建站模板源码系统是一套基于最新技术开发的网站建设解决方案。该系统集成了众多先进的网站开发工具和功能模块,旨在帮助企业快速构建出美观、实用的公司网站。通过采用模块化设计,系统实现了高度可定制性,企业可以根据自身…

一文读懂:RHEL如何通过control-center建立安全的IPsec VPN连接

大家好,这里是G-LAB IT实验室。虚拟专用网络(VPN)是一种通过互联网连接到本地网络的方法。Libreswan 提供的 IPsec 是创建 VPN 的首选方法。libreswan 是 VPN 的用户空间 IPsec 实现。VPN 通过在中间网络(如互联网)设置一个隧道来启用 LAN 和…

【Ubuntu-18.04.6 LTS (Bionic Beaver)】串口无法root登录解决方案

root用户无法再窗口登录 用户界面登录提示 soory that didnot work 解决方案 GDM 配置 /etc/gdm3/custom.conf 中增加或删除注释 [security] AllowRoottrue重启服务 service gdm restart确认 PAM 配置 GDM 使用 PAM 进行认证,可能 PAM 配置中限制了 root 登录…

22_单阶段目标检测-SSD算法理论

1.1 简介 SSD(Single Shot MultiBox Detector)是一种在深度学习领域广泛使用的对象检测算法,由Wei Liu等人在2015年提出。它是“单阶段”(one-stage)检测器的一个典型代表,与之相对的是像R-CNN系列这样的“…

【精简教程】VSCode 连接 Remix

初始化 Node.js 项目 yarn init v1.22.19安装 Remix yarn add remix-project/remixd -g⚠️ 此时如果直接敲 remix,显示找不到这个命令。 使用 Node.js 来直接执行 remixd.js 文件 node node_modules\remix-project\remixd\src\bin\remixd.js😄 连接上了…

软件供应链安全:如何防范潜在的攻击?

来源:https://thehackernews.com/2024/06/practical-guidance-for-securing-your.html 软件生产组织面临越来越大的监管和法律压力,要求其保护供应链并确保软件的完整性,这不足为奇。在过去几年里,软件供应链已经成为攻击者越来越…

【漏洞复现】docassemble——interview——任意文件读取

声明:本文档或演示材料仅供教育和教学目的使用,任何个人或组织使用本文档中的信息进行非法活动,均与本文档的作者或发布者无关。 文章目录 漏洞描述漏洞复现测试工具 漏洞描述 docassemble 是一款强大的开源工具,它让自动化生成和…

泛微e-cology getFileViewUrl接口存在SSRF漏洞复现 [附POC]

文章目录 泛微e-cology getFileViewUrl接口存在SSRF漏洞复现 [附POC]0x01 前言0x02 漏洞描述0x03 影响版本0x04 漏洞环境0x05 漏洞复现1.访问漏洞环境2.构造POC3.复现0x06 修复建议泛微e-cology getFileViewUrl接口存在SSRF漏洞复现 [附POC] 0x01 前言 免责声明:请勿利用文章…

运维圈都在“卷”的可观测性,还有这些要点运维人必须知道

在信息技术的快速发展下,IT运维领域在2018年迎来了一个全新的概念——“可观测性”(Observability),并迅速成为云原生技术领域的热点话题,被Gartner列为“2023年十大战略技术趋势”之一。Gartner预测,到202…

ipv4和ipv6的兼容性问题

ipv4和ipv6的兼容 现今大多知名网站都是同时支持ipv6和ipv4,这种可以分为两种情况讨论: 一个IPv4的网络和一个IPv6的网络通信;一个IPv6的网络和一一个IPv6的网络通信,但是中间需要经过一一个IPv4的网络。 先以第一种为例: 若一…

好玩的卡牌游戏推荐:堆叠大陆 Stacklands(Win/Mac)中文版

《堆叠大陆》是一款非常有趣和富有创造力的游戏,玩家可以在游戏中通过堆叠不同种类的方块来创建自己的世界。 在游戏中,玩家可以探索广阔的地图,发现各种不同的方块和资源。这些方块可以被堆叠在一起,形成各种建筑、结构和其他创…

单链表--续(C语言详细版)

2.6 在指定位置之前插入数据 // 在指定位置之前插入数据 void SLTInsert(SLTNode** pphead, SLTNode* pos, SLTDataType x); 分为两种情况:1. 插入的数据在链表中间;2. 插入的数据在链表的前面。 // 在指定位置之前插入数据 void SLTInsert(SLTNode** …

Python实战演练——羊了个羊抓包通关教程及无限套娃通关次数!

1. 需求 最近热门的羊了个羊,听说通关率不到0.1% 第一关超级简单,第二关可就难倒了太多的小伙伴,往往是你以为快没了,结果还有好多层,有朋友分析过,地狱有十八层,而它有十九层。 博主也曾熬夜…

可视化传输机房设计方案

建筑可视化机房可视化(3D)机房可视化(2D)多机柜可视化单机柜可视化(3D)单机柜可视化(2D)设备与插槽可视化端口连线及链路可视化

栈和队列题目详解

前言: 在前面我们学习了栈和队列,栈的特性是后进先出,队列的特性是先进先出,当我们了解了这些之后,我们就可以用到栈和队列的特性来简单的做一些题目了。 1. 有效的括号 有效的括号:. - 力扣&#xff08…

ESP32驱动摄像头:1.驱动OV2640模块(待验证)

一、装ArduCam库和ESPAsyncWebServer库 二、参考代码 #include <Wire.h> #include <ArduCAM.h> #include <SPI.h> #include <WiFi.h> #include <ESPAsyncWebServer.h>#define CAM_CS 32 // modify according to your own wiring #define OV2640…

昇思25天训练营Day11 - 基于 MindSpore 实现 BERT 对话情绪识别

模型简介 BERT全称是来自变换器的双向编码器表征量&#xff08;Bidirectional Encoder Representations from Transformers&#xff09;&#xff0c;它是Google于2018年末开发并发布的一种新型语言模型。与BERT模型相似的预训练语言模型例如问答、命名实体识别、自然语言推理、…

电焰灶:烹饪性能的深度剖析

在如今众多的厨房炉灶选择中&#xff0c;华火电焰灶以其独特的技术和性能吸引了不少消费者的目光。那么&#xff0c;华火电焰灶的综合烹饪性能究竟如何呢&#xff1f;让我们一起来深入探究。 首先&#xff0c;从火力方面来看&#xff0c;华火电焰灶展现出了强大的优势。其火焰强…

5G/4G加密边缘计算电力网关,开启智慧电力新篇章

计讯物联TG452&#xff0c;一款面向电力行业应用的工业级物联网网关&#xff0c;持电力协议及规约标准&#xff0c;支持采集、存储、算力、通信组网 、协议转换、控制等多功能。    电力应用   计讯物联电力网关TG452支持电力IEC101、IEC104、IEC61850、DL/T645等协议标准…