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