Skip to content

Instantly share code, notes, and snippets.

@max-lt
Last active November 22, 2022 21:55
Show Gist options
  • Save max-lt/5060cc6daac4cbe3794e549238e746a6 to your computer and use it in GitHub Desktop.
Save max-lt/5060cc6daac4cbe3794e549238e746a6 to your computer and use it in GitHub Desktop.
router-data.service.ts
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { Router, ActivatedRoute, Event, NavigationEnd } from '@angular/router';
import { filter, map, mergeMap, shareReplay } from 'rxjs/operators';
import { IRouteData } from '~/app/interfaces/route-data';
@Injectable({ providedIn: 'root' })
export class RouterDataService {
public readonly data$: Observable<IRouteData>;
constructor(router: Router, activatedRoute: ActivatedRoute) {
this.data$ = router.events.pipe(
filter((input: Event): input is NavigationEnd => input instanceof NavigationEnd),
map(() => activatedRoute),
map((route) => {
while (route.firstChild) {
route = route.firstChild;
}
return route;
}),
filter((route) => route.outlet === 'primary'),
mergeMap((route) => route.data),
shareReplay(1)
);
}
}
@Anna-Kozhel
Copy link

Please work

@max-lt
Copy link
Author

max-lt commented Nov 22, 2022

Pleasure ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment