Skip to content

Commit

Permalink
feat(app-ref): sync portal route when route change, rename navigateByUrl
Browse files Browse the repository at this point in the history
  • Loading branch information
why520crazy committed Sep 25, 2019
1 parent c09f7d7 commit dc104f9
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions packages/planet/src/application/planet-application-ref.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { PlanetRouterEvent, PlanetApplication } from '../planet.class';
import { PlanetPortalApplication } from './portal-application';
import { NgModuleRef, NgZone, ApplicationRef } from '@angular/core';
import { Router } from '@angular/router';
import { Router, NavigationEnd } from '@angular/router';
import { PlantComponentConfig } from '../component/plant-component.config';
import { PlanetComponentRef } from '../component/planet-component-ref';
import { take } from 'rxjs/operators';

declare const window: any;
export interface GlobalPlanet {
Expand Down Expand Up @@ -37,6 +38,23 @@ export class PlanetApplicationRef {
this.appModuleBootstrap = appModuleBootstrap;
}

// 子应用路由变化后同步修改 portal 的 Route
private syncPortalRouteWhenNavigationEnd() {
const router = this.appModuleRef.injector.get(Router);
const ngZone = this.appModuleRef.injector.get(NgZone);
if (router) {
router.events.subscribe(event => {
if (event instanceof NavigationEnd) {
ngZone.onStable
.asObservable()
.pipe(take(1))
.subscribe(() => {
this.portalApp.navigateByUrl(event.url);
});
}
});
}
}
async bootstrap(app: PlanetPortalApplication): Promise<void> {
if (!this.appModuleBootstrap) {
throw new Error(`${this.name} app is not define`);
Expand All @@ -45,15 +63,16 @@ export class PlanetApplicationRef {
return this.appModuleBootstrap(app).then(appModuleRef => {
this.appModuleRef = appModuleRef;
this.appModuleRef.instance.appName = this.name;
this.syncPortalRouteWhenNavigationEnd();
return;
});
}

onRouteChange(event: PlanetRouterEvent): void {
navigateByUrl(url: string): void {
const ngZone = this.appModuleRef.injector.get(NgZone);
const router = this.appModuleRef.injector.get(Router);
ngZone.run(() => {
router.navigateByUrl(event.url);
router.navigateByUrl(url);
});
}

Expand Down

0 comments on commit dc104f9

Please sign in to comment.