Angular 6 Breadcrumb

Usage

1. Install ng6-breadcrumbs via npm:

npm install --save ng6-breadcrumbs

2. Import the BreadcrumbsModule within your app (app.modules.ts) :

import {BreadcrumbsModule} from "ng6-breadcrumbs";
 
@NgModule({
  imports: [
    BreadcrumbsModule,
  ],
})

3. Add a name to your route by adding a breadcrumb property in the route's data(app-routing.module.ts or stores.module.ts):

const APP_ROUTES: Routes = [
    {path: '', redirectTo: 'home', pathMatch: 'full'},
    {
        path: 'home',
        loadChildren: 'app/home/home.module#HomeModule',
        data: {
            breadcrumb: 'Home'
        }
    },
    {
        path: 'stores',
        loadChildren: './modules/stores/stores.module#StoresModule',
        data: {
            breadcrumb: 'Stores'
        }
    }
];

or 

const STORE_ROUTES: Routes = [
  {
      path: '',
      component: StoresComponent
  },
  {
      path: 'books',
      data: {
          breadcrumb: 'Books'
      },
      loadChildren: '../books/books.module#BooksModule'
  }

];

4. Put the BreadcrumbsComponent's selector within your template (app.component.html):

5. add style.css to make breadcrumb become '>' style

6. Adding dynamic routes

In case you want a dynamic breadcrumb name, you can pass it as a :breadcrumb route parameter when navigating: Route:

Router Code:

Last updated

Was this helpful?