【发布时间】:2016-06-03 11:09:14
【问题描述】:
Angular2 路由器没有输出所有路由的方法,所以我所做的是定义一个服务来输出这样的路由...
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 }
];
}
}
现在我在我的main.ts 中使用它,就像@Routes(RouteService.getRoots()) 一样,一切似乎都正常,但是我想使用相同的服务来定义一个变量,这样我就可以循环遍历路由数组......就像这样
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....
但是,这会在控制台routeService.getRoots is not a function 中产生错误,并且我的应用程序崩溃了!我必须做什么才能在 main.ts 的 @Routes() 部分使用该服务,同时在其他组件中使用该服务。我真的不知道我哪里错了,因为 Angular2 现在正在改变我的想法!
【问题讨论】:
标签: typescript angular