import { Route , Router } from '@angular/router' ; import { Injectable } from '@angular/core' ; export interface NavRoute extends Route { path ? : string ; icon ? : string ; group ? : string ; groupedNavRoutes ? : NavRoute [ ] ; } export const sideNavPath = 'nav' ; export const navRoutes = [ { data : { title : 'Home' } , icon : 'home' , path : 'home' , loadChildren : ( ) => import ( './pages/home-page/home-page.module' ) . then ( m => m . HomePageModule ) , } , { path : '' , redirectTo : 'home' , pathMatch : 'full' , } , ] ; @ Injectable ( { providedIn : 'root' , } ) export class NavRouteService { navRoute : Route ; navRoutes : NavRoute [ ] ; constructor ( router ) { this . navRoute = router . config . find ( route => route . path === sideNavPath ) ; this . navRoutes = this . navRoute . children . filter ( route => route . data && route . data . title ) . reduce ( ( groupedList , route ) => { if ( route . group ) { const group = groupedList . find ( navRoute => { return ( navRoute . group === route . group && navRoute . groupedNavRoutes !== undefined ) ; } ) ; if ( group ) { group . groupedNavRoutes . push ( route ) ; } else { groupedList . push ( { group : route . group , groupedNavRoutes : [ route ] , } ) ; } } else { groupedList . push ( route ) ; } return groupedList ; } , [ ] ) ; } public getNavRoutes ( ) { return this . navRoutes ; } }