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,

通过打印,发现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; 
} 
}

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、需要修改下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; 
}
分享
微博
QQ
微信
回复
2024-08-04 13:36:42
相关问题
HAR 是否可以依赖 HSP?
117浏览 • 1回复 待解决
HarmonyOS HAR包之间的路由跳转
457浏览 • 1回复 待解决
HSP的命名路由跳转疑问
108浏览 • 1回复 待解决
HarmonyOS har依赖问题
452浏览 • 1回复 待解决
HarmonyOS router路由跳转问题
266浏览 • 0回复 待解决
HarmonyOS 路由跳转回调问题
203浏览 • 1回复 待解决
HarmonyOS module同时依赖同一个har
484浏览 • 1回复 待解决
HarmonyOS pushNamedRoute 跳转hsp问题
298浏览 • 1回复 待解决
router 路由跳转相关问题
138浏览 • 1回复 待解决
Navigation页面跳转问题
254浏览 • 1回复 待解决
Navigation管理页面堆栈太多问题
1731浏览 • 1回复 待解决
so相互依赖场景如何解耦
1879浏览 • 1回复 待解决
har 包多层依赖打包问题咨询
99浏览 • 1回复 待解决
HarmonyOS 路由跳转bea1的问题
227浏览 • 1回复 待解决
支付后的路由跳转问题
57浏览 • 1回复 待解决
没有依赖关系的HAR如何做页面跳转
275浏览 • 1回复 待解决