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

按照

https://developer.huawei.com/consumer/cn/doc/harmonyos-samples/samples-0000001162414961

中 “应用导航设计”的demo示例,我在项目新进了一个hsp包hspD,并参考C1新建了D1页面,Index也按照harC里面编写,在RouterModule新建了个一个routerpath,测试运行页面跳转D1时,报错

TypeError: Cannot read property builder of undefined,
  • 1.

通过打印,发现import了hspD项目的Index,是在加载D1页面的@Builder方法时报错问下,hsp该如何调整跳转成功,有无详细的demo

HarmonyOS
2024-08-03 13:11:52
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
沉默如海

请参考:

在本地试了下是可以跳转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; 
} 
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.

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); 
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.

3、需要修改下RouterModule模块utils.RouterModule.ets里面的代码,使用AppStorage去缓存路由相关数据,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; 
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
分享
微博
QQ
微信
回复
2024-08-04 13:36:42
相关问题
HAR 是否可以依赖 HSP?
1149浏览 • 1回复 待解决
HarmonyOS har包内部路由跳转
552浏览 • 1回复 待解决
HSP的命名路由跳转疑问
1033浏览 • 1回复 待解决
HarmonyOS 模块是怎样跳转的?
888浏览 • 1回复 待解决
HarmonyOS Navigation路由问题
706浏览 • 1回复 待解决
HarmonyOS HAR包之间的路由跳转
1375浏览 • 1回复 待解决
HarmonyOS har依赖问题
1528浏览 • 1回复 待解决
HarmonyOS router路由跳转问题
1433浏览 • 0回复 待解决
HarmonyOS module同时依赖同一个har
1736浏览 • 1回复 待解决
HarmonyOS pushNamedRoute 跳转hsp问题
995浏览 • 1回复 待解决
HarmonyOS 路由跳转回调问题
950浏览 • 1回复 待解决
router 路由跳转相关问题
1186浏览 • 1回复 待解决