|
1 |
| -import { Injectable, Injector } from '@angular/core'; |
2 |
| -import { Router, ActivatedRoute, NavigationEnd } from '@angular/router'; |
3 |
| -import { BehaviorSubject, Observable } from 'rxjs/index'; |
4 |
| -import { filter } from 'rxjs/operators'; |
5 |
| - |
6 |
| -@Injectable() |
7 |
| -export class AppBreadcrumbService { |
8 |
| - |
9 |
| - breadcrumbs: Observable<Array<Object>>; |
10 |
| - |
11 |
| - private _breadcrumbs: BehaviorSubject<Array<Object>>; |
12 |
| - |
13 |
| - constructor(private router: Router, private route: ActivatedRoute) { |
14 |
| - |
15 |
| - this._breadcrumbs = new BehaviorSubject<Object[]>(new Array<Object>()); |
16 |
| - |
17 |
| - this.breadcrumbs = this._breadcrumbs.asObservable(); |
18 |
| - |
19 |
| - this.router.events.pipe(filter(event => event instanceof NavigationEnd)).subscribe((event) => { |
20 |
| - const breadcrumbs = []; |
21 |
| - let currentRoute = this.route.root, |
22 |
| - url = ''; |
23 |
| - do { |
24 |
| - const childrenRoutes = currentRoute.children; |
25 |
| - currentRoute = null; |
26 |
| - // tslint:disable-next-line:no-shadowed-variable |
27 |
| - childrenRoutes.forEach(route => { |
28 |
| - if (route.outlet === 'primary') { |
29 |
| - const routeSnapshot = route.snapshot; |
30 |
| - url += '/' + routeSnapshot.url.map(segment => segment.path).join('/'); |
31 |
| - breadcrumbs.push({ |
32 |
| - label: route.snapshot.data, |
33 |
| - url: url |
34 |
| - }); |
35 |
| - currentRoute = route; |
36 |
| - } |
37 |
| - }); |
38 |
| - } while (currentRoute); |
39 |
| - |
40 |
| - this._breadcrumbs.next(Object.assign([], breadcrumbs)); |
41 |
| - |
42 |
| - return breadcrumbs; |
43 |
| - }); |
44 |
| - } |
45 |
| -} |
| 1 | +import { Injectable } from '@angular/core'; |
| 2 | +import { Router, ActivatedRoute, NavigationEnd } from '@angular/router'; |
| 3 | +import { BehaviorSubject, Observable } from 'rxjs/index'; |
| 4 | +import { filter } from 'rxjs/operators'; |
| 5 | + |
| 6 | +@Injectable() |
| 7 | +export class AppBreadcrumbService { |
| 8 | + |
| 9 | + breadcrumbs: Observable<Array<Object>>; |
| 10 | + |
| 11 | + private _breadcrumbs: BehaviorSubject<Array<Object>>; |
| 12 | + |
| 13 | + constructor(private router: Router, private route: ActivatedRoute) { |
| 14 | + |
| 15 | + this._breadcrumbs = new BehaviorSubject<Object[]>(new Array<Object>()); |
| 16 | + |
| 17 | + this.breadcrumbs = this._breadcrumbs.asObservable(); |
| 18 | + |
| 19 | + this.router.events.pipe(filter(event => event instanceof NavigationEnd)).subscribe((event) => { |
| 20 | + const breadcrumbs = []; |
| 21 | + let currentRoute = this.route.root, |
| 22 | + url = ''; |
| 23 | + do { |
| 24 | + const childrenRoutes = currentRoute.children; |
| 25 | + currentRoute = null; |
| 26 | + // tslint:disable-next-line:no-shadowed-variable |
| 27 | + childrenRoutes.forEach(route => { |
| 28 | + if (route.outlet === 'primary') { |
| 29 | + const routeSnapshot = route.snapshot; |
| 30 | + url += '/' + routeSnapshot.url.map(segment => segment.path).join('/'); |
| 31 | + breadcrumbs.push({ |
| 32 | + label: route.snapshot.data, |
| 33 | + url: url |
| 34 | + }); |
| 35 | + currentRoute = route; |
| 36 | + } |
| 37 | + }); |
| 38 | + } while (currentRoute); |
| 39 | + |
| 40 | + this._breadcrumbs.next(Object.assign([], breadcrumbs)); |
| 41 | + |
| 42 | + return breadcrumbs; |
| 43 | + }); |
| 44 | + } |
| 45 | +} |
0 commit comments