HarmonyOS router切换Nav?

nav怎么网络拦截呢

以前的网络请求是单独封装的

遇到401跳转登录页面,直接引用router就行,换成Nav怎么进行拦截跳转呢

new NavPathStack()这个能直使用吗

文档说用this.pageStack.setInterception拦截,但是在单独的文件方法函数中this使用报错。

而且对应的示例demo也没有,比如首次进入app和广告页,应该是用什么,首页又用什么,完全没有案例。文档描述的又感觉很懵。只知道怎么用,最外层的Navigation生命周期怎么接收第3个页面的返回参数,文档也没看到。至描述上个页面的返回

阅读 481
1 个回答

动态路由参考:

https://developer.huawei.com/consumer/cn/doc/best-practices-V5/bpta-application-navigation-design-V5\#section16766824123214

// RouterModule.ets

import { RouterModel } from '../model/RouterModel';
import Logger from './Logger';

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 {
    RouterModule.builderMap.set(builderName, builder);
  }

  // Get builder by name.
  public static getBuilder(builderName: string): WrappedBuilder<[object]> {
    const builder = RouterModule.builderMap.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 {
    RouterModule.routerMap.set(routerName, router);
  }

  // Get router by name.
  public static getRouter(routerName: string): NavPathStack {
    return RouterModule.routerMap.get(routerName) as NavPathStack;
  }

  // Jumping to a Specified Page by Obtaining the Page Stack.
  public static async push(router: RouterModel): Promise<void> {
    const harName = router.builderName.split('_')[0];
    // Dynamically import the page to be redirected to.
    await import(harName).then((ns: ESObject): Promise<void> => ns.harInit(router.builderName));
    RouterModule.getRouter(router.routerName).pushPath({ name: router.builderName, param: router.param });
  }

  // Obtain the page stack and pop it.
  public static pop(routerName: string): void {
    // Find the corresponding route stack for pop.
    RouterModule.getRouter(routerName).pop();
  }

  // Get the page stack and clear it.
  public static clear(routerName: string): void {
    // Find the corresponding route stack for pop.
    RouterModule.getRouter(routerName).clear();
  }

  // Directly jump to the specified route.
  public static popToName(routerName: string, builderName: string): void {
    RouterModule.getRouter(routerName).popToName(builderName);
  }
}

// RouterModel.ets
import { RouterModule } from '../utils/RouterModule';

// Routing information.
export class RouterModel {
  // Route page alias, in the form:${bundleName}_${pageName}.
  builderName: string = "";
  // Routing Stack Name.
  routerName: string = "";
  // Parameters that need to be transferred to the page.
  param?: object = new Object();
}

// Create routing information and put it into the routing stack table.
export function buildRouterModel(routerName: string, builderName: string, param?: object) {
  let router: RouterModel = new RouterModel();
  router.builderName = builderName;
  router.routerName = routerName;
  router.param = param;
  RouterModule.push(router);
}

// harB   index.ets
export function harInit(builderName: string): void {
  // 动态引入要跳转的页面
  switch (builderName) {
    case BuilderNameConstants.HARB_B1:
      import("./src/main/ets/components/mainpage/B1");
      break;
    case BuilderNameConstants.HARB_B2:
      import("./src/main/ets/components/mainpage/B2");
      break;
    case BuilderNameConstants.HARB_B3:
      import("./src/main/ets/components/mainpage/B3");
      break;
    default:
      break;
  }
}

// BuilderNameConstants.ets
export class BuilderNameConstants {
  static readonly HARA_A1: string = '@ohos/hara_A1';
  static readonly HARA_A2: string = '@ohos/hara_A2';
  static readonly HARB_B1: string = '@ohos/harb_B1';
  static readonly HARB_B2: string = '@ohos/harb_B2';
  static readonly HARB_B3: string = '@ohos/harb_B3';
  static readonly HARC_C1: string = '@ohos/harc_C1';
  static readonly HARC_C2: string = '@ohos/harc_C2';
}

// Indicates the key of the routerMap table in the RouterModule.
export class RouterNameConstants {
  static readonly ENTRY_HAP: string = 'EntryHap_Router';
}

Button(“跳转harb_pageB1”), { stateEffect: true, type: ButtonType.Capsule })
.width('80%')
  .height(40)
  .margin(20)
  .onClick(() => {
    buildRouterModel(RouterNameConstants.ENTRY_HAP, BuilderNameConstants.HARB_B1);
  })     
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进