我还需要动态生成链接。据我了解,问题在于路由器没有其他方法来生成链接,而是手动将它们放入[router-link]
。但是... 但是他们计划添加它们。路由器的队列中有很多功能,希望它们能尽快添加(
现在,我完成了这项工作-将routerConfig放在了装饰器的外面,因此可以在此组件中使用它(如果导出,也可以在其他组件中使用):
let routeConfig = [ { path: '/', name: 'Intro', component: IntroRouteComponent, useAsDefault: true }, ... { path: '/projects', name: 'Projects', component: ProjectsRouteComponent }, { path: '/about', name: 'About', component: AboutRouteComponent }, ]; @Component({ directives: [ROUTER_DIRECTIVES], providers: [], selector: 'app', template: ` {{ prevRoute }} {{ nextRoute }} `, }) @RouteConfig(routeConfig) export class AppComponent { private nextRoute: any; private prevRoute: any; constructor(private _router: Router) { this._router.subscribe(route => { let i = routeConfig.findIndex(r => r.path === ('/' + route)); this.prevRoute = routeConfig[i - 1] ? routeConfig[i - 1].name : false; this.nextRoute = routeConfig[i + 1] ? routeConfig[i + 1].name : false; }); } back() { this._router.navigate(Array(this.prevRoute)); } forward(): any { this._router.navigate(Array(this.nextRoute)); } }