HarmonyOS 鸿蒙Next Navigation组件下多har/hsp间路由跳转依赖耦合问题

发布于 1周前 作者 eggper 来自 鸿蒙OS

HarmonyOS 鸿蒙Next Navigation组件下多har/hsp间路由跳转依赖耦合问题

按照官方文档的示例项目:“应用导航设计”的demo示例,我在项目新进了一个hsp包hspD,并参考C1新建了D1页面,Index也按照harC里面编写,在RouterModule新建了个一个routerpath,测试运行页面跳转D1时,报错TypeError: Cannot read property builder of undefined,通过打印,发现import了hspD项目的Index,是在加载D1页面的@Builder方法时报错 问下,hsp该如何调整跳转成功,有无详细的demo

2 回复

在本地试了下是可以跳转HSP包的页面的,我的代码如下 1、hspd/index.ets:

import { BuilderNameConstants } from '@ohos/routermodule';

export function harInit(builderName: string): void {

  switch (builderName) {

    case BuilderNameConstants.HSPD_D1:

      import("./src/main/ets/pages/D1");

      break;

    default:

      break;

  }

}

2、hspd/src/main/ets/pages/D1.ets:

import { BuilderNameConstants, buildRouterModel, RouterModule, RouterNameConstants, } from '@ohos/routermodule';

@Builder

export function harBuilder(value: object) {

  NavDestination() {

    Column() {

      Text("D1")

    }

    .width('100%')

    .height('100%')

  }

  .title('D1Page')

  .onBackPressed(() => {

    RouterModule.pop(RouterNameConstants.ENTRY_HAP);

    return true;

  })

}

const builderName = BuilderNameConstants.HSPD_D1;

if (!RouterModule.getBuilder(builderName)) {

  let builder: WrappedBuilder<[object]> = wrapBuilder(harBuilder);

  RouterModule.registerBuilder(builderName, builder);

}

3、如果hsp中访问har包中的静态变量两次读出来的值不一样,可以通过APPStorage去规避这个问题。

export class RouterModule {

  static builderMap: Map<string, WrappedBuilder<[object]>> = new Map<string, WrappedBuilder<[object]>>();

  static routerMap: Map<string, NavPathStack> = new Map<string, NavPathStack>();

  // Registering a builder by name.

  public static registerBuilder(builderName: string, builder: WrappedBuilder<[object]>): void {

    let cacheBuilderMap: Map<string, WrappedBuilder<[object]>> | undefined = AppStorage.get('RouterBuildMap');

    if (!cacheBuilderMap) {

      cacheBuilderMap = new Map<string, WrappedBuilder<[object]>>();

    }

    cacheBuilderMap.set(builderName, builder);

    AppStorage.setOrCreate('RouterBuildMap', cacheBuilderMap);

  }

  // Get builder by name.

  public static getBuilder(builderName: string): WrappedBuilder<[object]> {

    let cacheBuilderMap: Map<string, WrappedBuilder<[object]>> | undefined = AppStorage.get('RouterBuildMap');

    if (!cacheBuilderMap) {

      cacheBuilderMap = new Map<string, WrappedBuilder<[object]>>();

    }

    const builder = cacheBuilderMap.get(builderName);

    if (!builder) {

      Logger.info('not found builder ' + builderName);

    }

    return builder as WrappedBuilder<[object]>;

  }

  // Registering a router by name.

  public static createRouter(routerName: string, router: NavPathStack): void {

    let cacheRouterMap: Map<string, NavPathStack> | undefined = AppStorage.get('RouterMap');

    if (!cacheRouterMap) {

      cacheRouterMap = new Map<string, NavPathStack>();

    }

    cacheRouterMap.set(routerName, router);

    AppStorage.setOrCreate('RouterMap', cacheRouterMap);

  }

  // Get router by name.

  public static getRouter(routerName: string): NavPathStack {

    let cacheRouterMap: Map<string, NavPathStack> | undefined = AppStorage.get('RouterMap');

    if (!cacheRouterMap) {

      cacheRouterMap = new Map<string

      // Get router by name.

      public static getRouter(routerName: string): NavPathStack {

        let cacheRouterMap: Map<string, NavPathStack> | undefined = AppStorage.get('RouterMap');

        if (!cacheRouterMap) {

          cacheRouterMap = new Map<string, NavPathStack>();

        }

        return cacheRouterMap.get(routerName) as NavPathStack;

      }
	  ```

针对HarmonyOS鸿蒙Next Navigation组件下多har/hsp间路由跳转依赖耦合问题,以下是一些专业解答:

首先,请确保已正确配置路由,包括目标页面的URL或路由名称。在HarmonyOS中,推荐使用Navigation组件来实现页面跳转,因其功能更加强大和完善。使用Navigation组件时,需设置导航页面列表和页面栈信息,并在点击事件中调用pushPath或相关方法进行跳转。

若需要在不同har包间进行页面跳转,需配置依赖并引入目标har包,或使用全局路由模块(如RouterModule)来管理路由表和路由栈,实现跨har包的页面跳转和按需加载。确保参数类型正确,并遵循HarmonyOS的传参规范。

在开发过程中,可能会遇到hsp中访问har包静态变量值不一致的问题,这可以通过APPStorage来规避。

如果在进行以上操作后问题依旧无法解决,可能是路由配置错误或代码实现有误,请仔细检查相关配置和代码。如果问题依旧没法解决请联系官网客服,官网地址是:https://www.itying.com/category-93-b0.html

回到顶部