完整示例参考如下:
class MyNavPathStack extends NavPathStack { removeByNavDestinationId(navDestinationId: string, result?: object): boolean { if (result) { this.notifyRemove(navDestinationId, result); } return super.removeByNavDestinationId(navDestinationId); } private notifyRemove(navDestinationId: string, result: object) { let remove = this.getPathStack().filter(item => item.navDestinationId === navDestinationId); if (remove.length <= 0) { return; } let info = remove[0]; info.onPop?.({ info: info, result: result }); // 调用onPop回调 } }
// 页面间传递的参数类型 class PageParams { name = ''; remove = ''; constructor(name?: string, remove?: string) { this.name = name === undefined ? '' : name; this.remove = remove === undefined ? '' : remove; } } @Entry @Component struct NavigationPage { pathStack: MyNavPathStack = new MyNavPathStack(); @Builder routeMap(param: string) { if (param === 'PageA') { PageA(); } else if (param === 'PageB') { PageB(); } else if (param === 'PageC') { PageC(); } } build() { Navigation(this.pathStack) { Button('跳转') .onClick(() => { this.pathStack.pushPath({ name: 'PageA', param: new PageParams('A') }); }); }.navDestination(this.routeMap); } } @Component struct PageA { pathStack: MyNavPathStack = new MyNavPathStack(); @State param: PageParams = new PageParams(); build() { NavDestination() { Column({ space: 10 }) { Text(`当前页面:` + this.param.name); Text(`待移除id:` + (this.param.remove || '-')); Button('开启监听并跳转').onClick(() => { // 跳转PageB页面并设置onPop回调 this.pathStack.pushPath({ name: 'PageB', param: new PageParams('B'), onPop: (res) => { console.info('A页面触发移除方法'); this.getUIContext() .showAlertDialog({ message: `页面${this.param.name}中触发的回调,参数为:${JSON.stringify(res)}` }); } }); }); }; }.onReady(context => { this.param = context.pathInfo.param as PageParams; this.pathStack = context.pathStack as MyNavPathStack; }); } } @Component struct PageB { pathStack: MyNavPathStack = new MyNavPathStack(); @State param: PageParams = new PageParams('', ''); private navDestinationId: string = ''; build() { NavDestination() { Column({ space: 10 }) { Text(`当前页面:` + this.param.name); Text(`待移除id:` + (this.param.remove || '-')); Button('跳转页面C').onClick(() => { // 跳转PageC页面并传递PageB的navDestinationId this.pathStack.pushPath({ name: 'PageC', param: new PageParams('C', this.navDestinationId) }); }); }; }.onReady(context => { this.param = context.pathInfo.param as PageParams; this.navDestinationId = context.navDestinationId!; this.pathStack = context.pathStack as MyNavPathStack; }); } } @Component struct PageC { pathStack: MyNavPathStack = new MyNavPathStack(); @State param: PageParams = new PageParams('', ''); build() { NavDestination() { Column({ space: 10 }) { Text(`当前页面:` + this.param.name); Text(`待移除id:` + (this.param.remove || '-')); if (this.param.remove) { Button('移除上一页面').onClick(() => { // 移除PageB页面,需要设置传递的参数,否则onPop回调不会触发 this.pathStack.removeByNavDestinationId(this.param.remove, new PageParams()); }); } }; }.onReady(context => { this.param = context.pathInfo.param as PageParams; this.pathStack = context.pathStack as MyNavPathStack; }); } }