
回复
使用HMRouter的页面跳转时,想实现和Navigation一样的生命周期时,需要通过新建生命周期类来实现对页面对某一个生命周期的监控。
通过继承IHMLifecycle接口实现生命周期接口的方法重写。
通过添加@HMLifecycle装饰器,来定义生命周期类的名称,然后在页面中使用
export interface IHMLifecycle {
onPrepare?(ctx: HMLifecycleContext): void;
onAppear?(ctx: HMLifecycleContext): void;
onDisAppear?(ctx: HMLifecycleContext): void;
onShown?(ctx: HMLifecycleContext): void;
onHidden?(ctx: HMLifecycleContext): void;
onWillAppear?(ctx: HMLifecycleContext): void;
onWillDisappear?(ctx: HMLifecycleContext): void;
onWillShow?(ctx: HMLifecycleContext): void;
onWillHide?(ctx: HMLifecycleContext): void;
onReady?(ctx: HMLifecycleContext): void;
onBackPressed?(ctx: HMLifecycleContext): boolean;
}
下面插入Navigation的生命周期流程图,HMRouter的生命周期流程类似,在此基础上增加了额外的生命周期流程。
export declare function HMLifecycle(param: HMLifecycleParam): ObjectConstructor;
export interface HMLifecycleParam {
lifecycleName: string;
priority?: number;
global?: boolean;
}
标记在实现了IHMLifecycle的对象上,声明此对象为一个自定义生命周期处理器。
在之前文章的基础上进行修改。
添加一个Lifecycles文件夹,并新建一个TwoPageLifecycle,来实现TwoPage页面的生命周期。
import { HMLifecycle, HMLifecycleContext, IHMLifecycle } from "@hadss/hmrouter";
@HMLifecycle({ lifecycleName: 'TwoPageLifecycle' })
export class TwoPageLifecycle implements IHMLifecycle {
/**
* 在拦截器执行后,路由栈真正push前触发
* @param ctx
*/
onPrepare(ctx: HMLifecycleContext): void {
console.debug("router", 'onPrepare');
}
onWillAppear(ctx: HMLifecycleContext): void {
console.debug("router", 'onWillAppear');
}
onAppear(ctx: HMLifecycleContext): void {
console.debug("router", 'onAppear');
}
onWillShow(ctx: HMLifecycleContext): void {
console.debug("router", 'onWillShow');
}
onShown(ctx: HMLifecycleContext): void {
console.debug("router", 'onShown');
}
onWillHide(ctx: HMLifecycleContext): void {
console.debug("router", 'onWillHide');
}
onHidden(ctx: HMLifecycleContext): void {
console.debug("router", 'onHidden');
}
onWillDisappear(ctx: HMLifecycleContext): void {
console.debug("router", 'onWillDisappear');
}
onDisAppear(ctx: HMLifecycleContext): void {
console.debug("router", 'onDisAppear');
}
onReady(ctx: HMLifecycleContext): void {
console.debug("router", 'onReady');
}
onBackPressed(ctx: HMLifecycleContext): boolean {
console.debug("router", 'onBackPressed');
return true;
}
}
import { HMPopInfo, HMRouter, HMRouterMgr } from '@hadss/hmrouter'
import { PageModel } from '../../Models/PageModel'
@HMRouter({ pageUrl: "TwoPage", lifecycle: "TwoPageLifecycle" })
@Component
export struct TwoPage {
aboutToAppear(): void {
let currentParam: PageModel = HMRouterMgr.getCurrentParam() as PageModel;
if (currentParam == undefined) {
return;
}
console.debug("router", 'name:' + currentParam.Name);
console.debug("router", 'age:' + currentParam.Age);
}
build() {
Column({ space: 20 }) {
Button("ThreePage")
.width("80%")
.onClick(() => {
HMRouterMgr.push({
navigationId: "mainNavigation",
pageUrl: "ThreePage"
}, {
onResult: (popInfo: HMPopInfo) => {
let popResult: PageModel = popInfo.result as PageModel;
if (popResult == null || popResult == undefined) {
return;
}
console.debug("router", 'name:' + popResult.Name);
console.debug("router", 'age:' + popResult.Age);
}
})
})
Button("ThreeReplacePage")
.width("80%")
.onClick(() => {
HMRouterMgr.replace({
navigationId: "mainNavigation",
pageUrl: "ThreePage"
}, {
onResult: (popInfo: HMPopInfo) => {
let popResult: PageModel = popInfo.result as PageModel;
if (popResult == null || popResult == undefined) {
return;
}
console.debug("router", 'name:' + popResult.Name);
console.debug("router", 'age:' + popResult.Age);
}
})
})
Button("HomePage")
.width("80%")
.onClick(() => {
HMRouterMgr.pop({
navigationId: "mainNavigation"
})
})
}
.height("100%")
.width("100%")
}
}
在生命周期方法中实现内容打印,截图如下:
可以看到生命周期的调用顺序