66 * found in the LICENSE file at https://angular.dev/license
77 */
88
9- import { ChangeDetectionStrategy , Component , OnInit , inject , signal } from '@angular/core' ;
9+ import { ChangeDetectionStrategy , Component , inject , computed } from '@angular/core' ;
1010import { NavigationState } from '../../services/index' ;
1111import { NavigationItem } from '../../interfaces/index' ;
1212import { RouterLink } from '@angular/router' ;
@@ -18,31 +18,18 @@ import {RouterLink} from '@angular/router';
1818 styleUrls : [ './breadcrumb.component.scss' ] ,
1919 changeDetection : ChangeDetectionStrategy . OnPush ,
2020} )
21- export class Breadcrumb implements OnInit {
21+ export class Breadcrumb {
2222 private readonly navigationState = inject ( NavigationState ) ;
2323
24- breadcrumbItems = signal < NavigationItem [ ] > ( [ ] ) ;
24+ breadcrumbItems = computed ( ( ) => {
25+ const breadcrumbs : NavigationItem [ ] = [ ] ;
26+ let activeItem = this . navigationState . activeNavigationItem ( ) ?. parent ;
2527
26- ngOnInit ( ) : void {
27- this . setBreadcrumbItemsBasedOnNavigationStructure ( ) ;
28- }
28+ while ( activeItem != null ) {
29+ breadcrumbs . push ( activeItem ) ;
30+ activeItem = activeItem . parent ;
31+ }
2932
30- private setBreadcrumbItemsBasedOnNavigationStructure ( ) : void {
31- let breadcrumbs : NavigationItem [ ] = [ ] ;
32-
33- const traverse = ( node : NavigationItem | null ) => {
34- if ( ! node ) {
35- return ;
36- }
37-
38- if ( node . parent ) {
39- breadcrumbs = [ node . parent , ...breadcrumbs ] ;
40- traverse ( node . parent ) ;
41- }
42- } ;
43-
44- traverse ( this . navigationState . activeNavigationItem ( ) ) ;
45-
46- this . breadcrumbItems . set ( breadcrumbs ) ;
47- }
33+ return breadcrumbs . reverse ( ) ;
34+ } ) ;
4835}
0 commit comments