1- import { Component , Input , OnChanges , SimpleChanges } from '@angular/core' ;
2- import { Router } from '@angular/router' ;
1+ import {
2+ Component ,
3+ EventEmitter ,
4+ Input ,
5+ OnChanges ,
6+ OnDestroy ,
7+ Output ,
8+ SimpleChanges ,
9+ } from '@angular/core' ;
10+ import { Router , RoutesRecognized } from '@angular/router' ;
311import { TranslateService } from '@ngx-translate/core' ;
12+ import { Subscription } from 'rxjs' ;
13+
14+ export interface RouterEvent {
15+ url : string ;
16+ replaceUrl : boolean ;
17+ }
418
519@Component ( {
620 template : '<mf-booking></mf-booking>' ,
721} )
8- export class EntryComponent implements OnChanges {
22+ export class EntryComponent implements OnChanges , OnDestroy {
923 @Input ( ) route ?: string ;
24+ @Output ( ) routeChange = new EventEmitter < RouterEvent > ( ) ;
1025
1126 @Input ( ) language ?: 'en' | 'de' ;
1227
28+ private subscription : Subscription ;
29+
1330 constructor (
1431 private router : Router ,
1532 private translateService : TranslateService
16- ) { }
33+ ) {
34+ const routingSubscription = this . registerOutgoingRouting ( ) ;
35+ this . subscription = routingSubscription ;
36+ }
1737
1838 ngOnChanges ( changes : SimpleChanges ) {
1939 if ( changes . route && this . route ) {
@@ -23,4 +43,30 @@ export class EntryComponent implements OnChanges {
2343 this . translateService . use ( this . language ) ;
2444 }
2545 }
46+
47+ ngOnDestroy ( ) : void {
48+ this . subscription . unsubscribe ( ) ;
49+ }
50+
51+ private registerOutgoingRouting ( ) : Subscription {
52+ return this . router . events . subscribe ( ( event ) => {
53+ if (
54+ event instanceof RoutesRecognized &&
55+ ( ! this . isRouteChangeFromPlatform ( ) || this . isRedirect ( event ) )
56+ ) {
57+ this . routeChange . next ( {
58+ url : event . urlAfterRedirects ,
59+ replaceUrl : this . isRedirect ( event ) ,
60+ } ) ;
61+ }
62+ } ) ;
63+ }
64+
65+ private isRouteChangeFromPlatform ( ) : boolean {
66+ return this . router . getCurrentNavigation ( ) ?. extras ?. state ?. fromPlatform ;
67+ }
68+
69+ private isRedirect ( event : RoutesRecognized ) : boolean {
70+ return event . url !== event . urlAfterRedirects ;
71+ }
2672}
0 commit comments