Skip to content

Commit e5a9def

Browse files
authored
Merge pull request coreui#6 from jblackburn21/breadcrumbObservable
fix: coreui#5 update breadcrumbs when route changes
2 parents 8f5bc24 + 9220140 commit e5a9def

File tree

2 files changed

+46
-39
lines changed

2 files changed

+46
-39
lines changed

src/breadcrumb/app-breadcrumb.component.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { AppBreadcrumbService } from './app-breadcrumb.service';
55
@Component({
66
selector: 'app-breadcrumb',
77
template: `
8-
<ng-template ngFor let-breadcrumb [ngForOf]="breadcrumbs" let-last = last>
8+
<ng-template ngFor let-breadcrumb [ngForOf]="breadcrumbs | async" let-last = last>
99
<li class="breadcrumb-item"
1010
*ngIf="breadcrumb.label.title&&breadcrumb.url.substring(breadcrumb.url.length-1) == '/'||breadcrumb.label.title&&last"
1111
[ngClass]="{active: last}">
@@ -25,9 +25,6 @@ export class AppBreadcrumbComponent implements OnInit {
2525
Replace(this.el);
2626
this.isFixed(this.fixed);
2727
this.breadcrumbs = this.service.breadcrumbs;
28-
// const s = this.service.crumbs$.subscribe((x) => {
29-
// this.crumbs = x;
30-
// });
3128
}
3229

3330
isFixed(fixed: boolean): void {
Lines changed: 45 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,45 @@
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

Comments
 (0)