import { Injectable } from '@angular/core' ; import { Router , RoutesRecognized } from '@angular/router' ; import { Observable , Subject , Subscription } from 'rxjs' ; import { distinctUntilChanged } from 'rxjs/operators' ; import { RouterEvent } from './router-event.type' ; function isRedirect ( event ) { return event . url !== event . urlAfterRedirects ; } Input Output @ Injectable ( { providedIn : 'root' } ) export class EntryRoutingService { constructor ( private router ) { } registerRouting ( outgoingRoute$ , incomingRoute$ ) { const inSubscription = this . registerIncomingRouting ( incomingRoute$ ) ; const outSubscription = this . registerOutgoingRouting ( outgoingRoute$ ) ; return inSubscription . add ( outSubscription ) ; } registerIncomingRouting ( incomingRoute$ ) { return incomingRoute$ . pipe ( distinctUntilChanged ( ) ) . subscribe ( ( route ) => { if ( route ) { this . router . navigateByUrl ( route , { state : { fromPlatform : true } } ) ; } } ) ; } registerOutgoingRouting ( outgoingRoute$ ) { return this . router . events . subscribe ( ( event ) => { if ( event instanceof RoutesRecognized && ( ! this . isFromPlatform ( ) || isRedirect ( event ) ) ) { outgoingRoute$ . next ( { url : event . urlAfterRedirects , replaceUrl : isRedirect ( event ) , } ) ; } } ) ; } isFromPlatform ( ) { return this . router . getCurrentNavigation ( ) ?. extras ?. state ?. fromPlatform ; } }