There's no method with the Angular2 router to output all the routes, so what I have done is defined a Service to output the routes like so...
export class Route {
constructor(
public path: string,
public component: any
) {}
}
@Injectable()
export class RouteService {
public static getRoots(): Route[] {
return [
{ path: '/', component: HomeComponent },
{ path: '/about-me', component: AboutMeComponent },
{ path: '/food', component: FoodComponent },
{ path: '/photos', component: PhotosComponent },
{ path: '/technology', component: TechnologyComponent },
{ path: '/blog', component:BlogComponent }
];
}
}
Now I use this in my main.ts like so @Routes(RouteService.getRoots()) and everything seems to work, however I would like to use the same service to define a variable so I can loop through the array of routes... like so
export class NavigationComponent {
public routes: Route[];
constructor(private router:Router, private routeService: RouteService) {
// just write this to the console for now
console.log(routeService.getRoots());
}
// more code here, etc....
However this produces an error in the console routeService.getRoots is not a function and my app crashes! What must I do so I can use the service in the @Routes() section of my main.ts whilst using the service in other components. I really don't know where I am going wrong as Angular2 is bending my mind right now!