Skip to content

Commit dc104f9

Browse files
committed
feat(app-ref): sync portal route when route change, rename navigateByUrl
1 parent c09f7d7 commit dc104f9

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

packages/planet/src/application/planet-application-ref.ts

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { PlanetRouterEvent, PlanetApplication } from '../planet.class';
22
import { PlanetPortalApplication } from './portal-application';
33
import { NgModuleRef, NgZone, ApplicationRef } from '@angular/core';
4-
import { Router } from '@angular/router';
4+
import { Router, NavigationEnd } from '@angular/router';
55
import { PlantComponentConfig } from '../component/plant-component.config';
66
import { PlanetComponentRef } from '../component/planet-component-ref';
7+
import { take } from 'rxjs/operators';
78

89
declare const window: any;
910
export interface GlobalPlanet {
@@ -37,6 +38,23 @@ export class PlanetApplicationRef {
3738
this.appModuleBootstrap = appModuleBootstrap;
3839
}
3940

41+
// 子应用路由变化后同步修改 portal 的 Route
42+
private syncPortalRouteWhenNavigationEnd() {
43+
const router = this.appModuleRef.injector.get(Router);
44+
const ngZone = this.appModuleRef.injector.get(NgZone);
45+
if (router) {
46+
router.events.subscribe(event => {
47+
if (event instanceof NavigationEnd) {
48+
ngZone.onStable
49+
.asObservable()
50+
.pipe(take(1))
51+
.subscribe(() => {
52+
this.portalApp.navigateByUrl(event.url);
53+
});
54+
}
55+
});
56+
}
57+
}
4058
async bootstrap(app: PlanetPortalApplication): Promise<void> {
4159
if (!this.appModuleBootstrap) {
4260
throw new Error(`${this.name} app is not define`);
@@ -45,15 +63,16 @@ export class PlanetApplicationRef {
4563
return this.appModuleBootstrap(app).then(appModuleRef => {
4664
this.appModuleRef = appModuleRef;
4765
this.appModuleRef.instance.appName = this.name;
66+
this.syncPortalRouteWhenNavigationEnd();
4867
return;
4968
});
5069
}
5170

52-
onRouteChange(event: PlanetRouterEvent): void {
71+
navigateByUrl(url: string): void {
5372
const ngZone = this.appModuleRef.injector.get(NgZone);
5473
const router = this.appModuleRef.injector.get(Router);
5574
ngZone.run(() => {
56-
router.navigateByUrl(event.url);
75+
router.navigateByUrl(url);
5776
});
5877
}
5978

0 commit comments

Comments
 (0)