当前位置: 首页 > wzjs >正文

网站开发量软件开发培训机构

网站开发量,软件开发培训机构,经营网站挣钱,国外有哪做交互设计网站Harmonyos-Navigation路由跳转 概述Navigation路由跳转模块内页面路由系统路由表测试页代码创建并配置路由表文件配置创建好的路由表文件跳转页面 自定义路由表 跨模块路由封装库模块路由跳转工具类 概述 Navigation是路由容器组件,一般作为首页的根容器&#xff0…

Harmonyos-Navigation路由跳转

  • 概述
  • Navigation路由跳转
    • 模块内页面路由
      • 系统路由表
        • 测试页代码
        • 创建并配置路由表文件
        • 配置创建好的路由表文件
        • 跳转页面
      • 自定义路由表
    • 跨模块路由
    • 封装库模块路由跳转工具类

概述

Navigation是路由容器组件,一般作为首页的根容器,适用于模块内和跨模块的路由切换。Navigation子页是NavDestination包含的子组件,使用NavPathStack实现页面路由。

Navigation路由跳转

模块内页面路由

Index.ets跟页面代码:

  @Entry@Componentstruct Index {/*** 创建一个页面栈对象*/pathStack: NavPathStack = new NavPathStack()build() {Navigation(this.pathStack) {Column() {Text("这是一个test Navigation Page的例子")Button("点击跳转").onClick((event: ClickEvent) => {})}.height('100%').width('100%')}.title('Navigation Main Page')}}

系统路由表

系统路由表的方式,不需要引入子页面的ets文件,解除了各个页面之间的耦合;此外路由跳转通过配置文件配置方式实现,降低了代码的复杂度。建议使用系统路由表的方式实现页面的跳转。

测试页代码

PageOne.ets测试页的代码:

@Builder
export function PageOneBuilder() {PageOne()
}
@Component
struct PageOne {pathStack: NavPathStack = new NavPathStack();build() {NavDestination() {Column() {Text("PageOne Test")}}.title("PageOne").onReady((context: NavDestinationContext) => {this.pathStack = context.pathStack;})}
}
创建并配置路由表文件

我们需要配置系统路由表, 在resource/base/profile目录下创建一个路由的配置文件:
在这里插入图片描述
其内容如下:

{"routerMap": [{"name": "PageOne","pageSourceFile": "src/main/ets/pages/PageOne.ets","buildFunction": "PageOneBuilder","data": {"des": "传递的参数"}}]
}
配置创建好的路由表文件

然后需要再module.json5的配置文件中的module标签中定义routeMap字段, 指向定义好的路由表配置文件route_map.json

在这里插入图片描述

跳转页面
  Button("点击跳转").onClick((event: ClickEvent) => {// 名称是路由表注册的名称         this.pathStack.pushPathByName("PageOne", null, false)})

自定义路由表

我们需要修改Index.ets文件内容如下:

import { PageTwo } from './PageTwo'@Entry
@Component
struct Index {/*** 创建一个页面栈对象*/pathStack: NavPathStack = new NavPathStack()@BuilderpageMap(pageName: string, param: ESObject) {if (pageName === "PageTwo") {PageTwo()}} build() {Navigation(this.pathStack) {Column() {Text("这是一个test Navigation Page的例子")Button("点击跳转").onClick((event: ClickEvent) => {this.pathStack.pushPathByName("PageTwo", null, false)})}.height('100%').width('100%')}.title('Navigation Main Page').navDestination(this.pageMap)}
}

这种路由方式,不需要配置什么文件。

更多路由页面的其他操作参考链接:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V14/ts-basic-components-navigation-V14

跨模块路由

对于其他模块的页面,如果需要跨模块进行路由跳转, 首先我们对于该模块的页面进行系统路由的配置操作。配置步骤和上面是一样的。

NavPushPathHelper对Navigation路由栈NavPathStack的所有路由跳转接口进行了封装,在NavPushPathHelper中持有一个NavPathStack对象,在封装的跳转接口中去判断子包是否存在,如果不存在则进行动态下载子包,等结果返回后调用NavPathStack的相应的接口将指定的NavDestination页面信息入栈。

如:
在这里插入图片描述
在这里插入图片描述
配置好路由表之后, 我们在需要跳转的地方, 实现如下逻辑:

/*** 创建一个页面栈对象*/pathStack: NavPathStack = new NavPathStack()helper: NavPushPathHelper = new NavPushPathHelper(this.pathStack)Button("点击跳转另外一个har的页面").onClick((event: ClickEvent) => {// NavigationHelper.pushPathByName({ name: "PageOne"})this.helper.pushPath("producehar", { name: 'MainPage' }, false).then(() => {console.info('pushPath success')}).catch((error: BusinessError) => {console.error('pushPath error: ' + JSON.stringify(error))})})Button("点击跳转另外一个hsp的页面").onClick((event: ClickEvent) => {this.helper.pushPath("hsptest", { name: 'Index' }, false).then(() => {console.info('pushPath success')}).catch((error: BusinessError) => {console.error('pushPath error: ' + JSON.stringify(error))})})

封装库模块路由跳转工具类

工具类代码如下:

import { bundleManager } from '@kit.AbilityKit';
import { NavPushPathHelper } from '@kit.ArkUI';
import { BusinessError } from '@kit.BasicServicesKit';
import { JSON } from '@kit.ArkTS';/*** @Author: You name* @CreateDate: 2025/4/9 18:24* @Description: 类功能描述说明*/
class NavigationUtils {public navPathStack: NavPathStack;private navModuleNameMap: Map<string, string>;private helper: NavPushPathHelper;constructor() {const bundleInfo = this.getBundleInfo();this.navModuleNameMap = this.getNavModuleNameMap(bundleInfo);this.navPathStack = new NavPathStack();this.helper = new NavPushPathHelper(this.navPathStack);}/** 获取包信息* */private getBundleInfo() {// 实现获取包信息的逻辑const bundleFlags =bundleManager.BundleFlag.GET_BUNDLE_INFO_DEFAULT |bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_APPLICATION |bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_HAP_MODULE |bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_ABILITY |bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_EXTENSION_ABILITY |bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_REQUESTED_PERMISSION |bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_METADATA |bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_DISABLE |bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_SIGNATURE_INFO |bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_MENU |bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_ROUTER_MAP |bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_SKILLreturn bundleManager.getBundleInfoForSelfSync(bundleFlags);}/*** 获取导航模块名称映射* */private getNavModuleNameMap(bundleInfo: bundleManager.BundleInfo) {// 实现获取导航模块名称映射的逻辑const moduleNameMap = new Map<string, string>();bundleInfo.hapModulesInfo.forEach((moduleInfo) => {moduleInfo.routerMap.forEach((router) => {moduleNameMap.set(router.name, moduleInfo.name);});});return moduleNameMap;}/*** 跳转到指定页面*/public pushPathByName(info: NavPathInfo, animation?: boolean) {// 获取需要跳转的模块名称const moduleName = this.navModuleNameMap.get(info.name);if (moduleName) {// 实现跳转逻辑this.helper.pushPath(moduleName, info, animation).then(() => {// 跳转成功后的回调逻辑console.log('Push path success=============');}).catch((error: BusinessError) => {// 跳转失败后的回调逻辑console.error('Error: ===================' + error.message);})} else {console.error('Module name not found======================');}}/*** 根据Navigation路由名称, 查找携带参数* @param name 导航路由名称*/public getPathByName<NaDestinationParam>(name: string): NaDestinationParam[] | undefined {// 实现查找逻辑if (!this.navPathStack) {console.error('NavPathStack is null');return undefined;}return this.navPathStack.getParamByName(name) as NaDestinationParam[];}/*** 路由后退* @param name* @param result* @param animation*/public pop<T = null>(name?: string, result?: T, animation?: boolean) {if (!this.navPathStack) {console.error('NavPathStack is null');return;}if (name) {this.navPathStack.popToName(name, result, animation)} else {this.navPathStack.pop()}}/*** 获取父级页面信息*/public gteParent() {const allPathName = this.navPathStack.getAllPathName();if (allPathName.length === 0) {return undefined;}console.log("allPathName==============" + JSON.stringify(allPathName));return allPathName[allPathName.length - 1];}/*** 路由清除 回到根页面*/public clear(animation?: boolean) {if (!this.navPathStack) {console.error('NavPathStack is null');return;}this.navPathStack.clear(animation);}public gteLastPathName(): string | undefined {if (!this.navPathStack) {console.error('NavPathStack is null');return undefined;}const allPathName = this.navPathStack.getAllPathName();if (allPathName.length === 0) {return undefined;}console.log("allPathName==============" + JSON.stringify(allPathName));return allPathName[allPathName.length - 1];}
}const NavigationHelper = new NavigationUtils();export { NavigationHelper };

使用方式:

 build() {// 把工具类中的navPathStack传递进去Navigation(NavigationHelper.navPathStack) {Column() {Text("这是一个test Navigation Page的例子")Button("跳转本模块的页面").onClick((event: ClickEvent) => {NavigationHelper.pushPathByName({ name: "PageOne"})})Button("点击跳转另外一个har的页面").onClick((event: ClickEvent) => {NavigationHelper.pushPathByName({ name: "MainPage" })})Button("点击跳转另外一个hsp的页面").onClick((event: ClickEvent) => {NavigationHelper.pushPathByName({ name: "Index" })})}.height('100%').width('100%')}
http://www.dtcms.com/wzjs/168594.html

相关文章:

  • 济南网站建设哪家公司好青岛网站建设有限公司
  • 廊坊市网站青海seo技术培训
  • 国家企业信息网官网查询系统沈阳网络seo公司
  • 网站seo公司重庆seo网络推广
  • 网站登录密码怎么取消保存seo搜索引擎优化薪资水平
  • 引蜘蛛网站信息流优化师简历模板
  • 网站设计规划思路重庆网站推广联系方式
  • 网站做鸭宁波seo外包优化
  • 做私活一个网站大概多少钱泰安短视频seo
  • 常州地区网页制作公司优化百度涨
  • ubuntu怎么安装wordpress常州百度关键词优化
  • 网站空间租用费用网络推广的优势
  • 河北邯郸手机网站建设在线培训app
  • 徐州企业建站系统模板网络推广公司运作
  • 前端做视频直播网站aso优化{ }贴吧
  • 和两个黑人同时做网站百度指数分析案例
  • 可以做微网站的第三方平台如何提高百度搜索排名
  • 网站建设上传和下载代写文章平台
  • 怎做直销网站网店运营入门基础知识
  • 校园门户网站设计论文国内企业网站模板
  • 旅游网站的建设的意义google play下载官方版
  • 青海建设云网站东莞网站建设市场
  • 网站备案空间备案吗金华网站建设
  • 云南做网站哪家便宜正规网站优化推广
  • 难道做网站的工资都不高吗惠州seo排名优化
  • 大庆网站建设晚上看b站
  • 淮安做网站就找卓越凯欣蜗牛精灵seo
  • 溧阳有没有做网站的公司seo排名优化软件免费
  • 做网站买过域名之后福建seo
  • 文山北京网站建设淄博信息港聊天室网址