Last active
November 22, 2022 21:55
-
-
Save max-lt/5060cc6daac4cbe3794e549238e746a6 to your computer and use it in GitHub Desktop.
router-data.service.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Please work