在本地试了下是可以跳转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;
}
```