import { Injectable } from '@angular/core'; import { Router, ActivatedRoute, NavigationEnd, Params, Data } from '@angular/router'; import { Observable, ReplaySubject, Subject } from 'rxjs'; @Injectable() export class RoutingService { constructor(private router: Router, private activeRoute: ActivatedRoute) { this.getParams(); } public isLocationDashboard: boolean; private _accountId: string; public _installationId: string; public _customerId: string; private _routeParams: Params; private _routeData: Data; private routeParamsSource: ReplaySubject; private routeDataSource: Subject; public accountIdSubject: ReplaySubject = new ReplaySubject(1); public installationIdSubject: ReplaySubject = new ReplaySubject(1); public customerIdSubject: ReplaySubject = new ReplaySubject(1); public set accountId(value: string) { if (value && value !== this._accountId) { this.accountIdSubject.next(value); } this._accountId = value; }; public get accountId(): string { return this._accountId; } public set installationId(value: string) { if (value && value !== this._installationId) { this.installationIdSubject.next(value); } this._installationId = value; }; public get installationId(): string { return this._installationId; } public set customerId(value: string) { if (value && value !== this._customerId) { this.customerIdSubject.next(value); } this._customerId = value; }; public get customerId(): string { return this._customerId; } public get routeParams(): Observable { if (this.routeParamsSource == null) { this.routeParamsSource = new ReplaySubject(1); if (this._routeParams) { this.routeParamsSource.next(this._routeParams); } } return this.routeParamsSource; } public get routeData(): Observable { if (this.routeDataSource == null) { this.routeDataSource = new Subject(); if (this._routeData) { this.routeDataSource.next(this._routeData); } } return this.routeDataSource; } private getParams(): void { this.router.events .filter(event => event instanceof NavigationEnd) .subscribe(event => { this._routeParams = this.activeRoute.firstChild.snapshot.params; this._routeData = this.activeRoute.firstChild.snapshot.data; if (this.routeParamsSource) { this.routeParamsSource.next(this._routeParams); } if (this.routeDataSource) { this.routeDataSource.next(this._routeData); } }); } }