【发布时间】:2016-08-21 18:03:48
【问题描述】:
我正在尝试在 Angular2 中创建嵌套路由。
这是我的组件的代码: app.partial.html
<nav class="navbar navbar-default">
<div class="container-fluid">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">Brand</a>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li><a [routerLink]="['Git']">Git Cmp</a></li>
<li><a [routerLink]="['Courses']">Courses Cmp</a></li>
<li><a [routerLink]="['Archives']">Archives Cmp</a></li>
</ul>
</div>
</div>
这是我的主要视图,我在其中包含了指向子组件的[routerLink]=['Archieves'] 链接及其自己的路由。
app.component.ts
@RouteConfig([
{ path: '/git', name: 'Git', component: GitComponent },
{ path: '/courses', name: 'Courses', component: CoursesComponent, useAsDefault: true },
{ path: '/archives/...', name: 'Archives', component: ArchivesComponent },
{ path: '/*other', name: 'Other', redirectTo: ['Git'] }
])
这里是父组件路由配置,其中包括 /archives/... 路由,它期望子路由。
archives.component.ts
@RouteConfig([
{ path: '/:id', name: 'Archive', component: ArchiveComponent, useAsDefault: true }
])
您可以在上面看到子组件的路由配置。如你所见,它有id 参数,所以当我们重定向到这个路由时,它期望一些值。
问题
问题是:有没有办法为路由参数设置默认值。因为我的链接导致的错误未包含所需的路由参数。
例外:“id”的路由生成器未包含在传递的参数中。在 [['Archives'] in AppComponent@18:23]
感谢您的帮助!
【问题讨论】:
-
你在说什么重定向?重定向到
Git,但错误来自Archive。什么操作会产生此错误? -
Archives路由产生的错误,不要关注其他路由,它们按预期工作。问题出在archives->archive路由中。