11import { PlanetRouterEvent , PlanetApplication } from '../planet.class' ;
22import { PlanetPortalApplication } from './portal-application' ;
33import { NgModuleRef , NgZone , ApplicationRef } from '@angular/core' ;
4- import { Router } from '@angular/router' ;
4+ import { Router , NavigationEnd } from '@angular/router' ;
55import { PlantComponentConfig } from '../component/plant-component.config' ;
66import { PlanetComponentRef } from '../component/planet-component-ref' ;
7+ import { take } from 'rxjs/operators' ;
78
89declare const window : any ;
910export 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