【发布时间】:2016-03-07 18:18:00
【问题描述】:
我有一个具有@Routeconfig(父级)的组件。
import {Component} from "angular2/core";
import {RouteConfig,ROUTER_DIRECTIVES, RouterOutlet} from 'angular2/router';
import {Theme} from "../theme/theme";
import {Router,RouteParams} from "angular2/router";
import {OrganisationService} from "./organisation.service";
import {Organisation} from "./organisation";
import {OrganisationComponent} from "./organisation.component";
import {EmptyComponent} from "../../empty.component";
import {ThemeListComponent} from "../theme/theme-list.component";
@Component({
template: `
<div class="container">
<ul>
<li *ngFor="#organisation of organisations">
<a [routerLink]="['./ThemeList',{ organisationId: organisation.organisationId }]" >{{organisation.organisationName}}</a>
</li>
</ul>
</div>
<router-outlet></router-outlet>
`,
directives:[RouterOutlet],
providers:[OrganisationService]
})
@RouteConfig([
{path: '/', name:'Empty', component:EmptyComponent,useAsDefault: true},
{path: '/:organisationId/Themes/...', name:'ThemeList', component:ThemeListComponent}
])
export class OrganisationListComponent {
public organisations:Organisation[];
constructor(private organisationService:OrganisationService) {
this.organisationService.getOrganisations().subscribe((organisations:Organisation[])=> {
this.organisations = organisations;
});
}
}
我想将组织中的 id 发送给我的ThemeListComponent(孩子)。这可行,但是当我尝试使用我的子组件获取 routeParams 时,它会出错。
例外:路由器实例化期间出错! (RouterLink -> 路由器)。
原始例外:路由配置应该只包含一个“组件”、“加载器”或“重定向到”属性。
这是 ThemeListComponent 以及我如何尝试接收 routerParams:
import {Component} from "angular2/core";
import {RouteConfig,ROUTER_DIRECTIVES, RouterOutlet} from 'angular2/router';
import {Theme} from "../theme/theme";
import {Router,RouteParams} from "angular2/router";
import {ThemeComponent} from "../theme/theme.component";
import {ThemeService} from "./theme.service";
import {EmptyComponent} from "../../empty.component";
@Component({
template: `
<div class="container">
<ul>
<li *ngFor="#theme of themes">
<a [routerLink]="['./Theme',{ themeId: theme.themeId }]">{{theme.name}}</a>
</li>
</ul>
</div>
<router-outlet></router-outlet>
`,
directives:[RouterOutlet,RouteParams],
providers:[ThemeService]
})
@RouteConfig([
{path: '/', name:'Empty', component:EmptyComponent,useAsDefault: true},
{path: '/:themeId', name:'Theme', component:ThemeComponent}
])
export class ThemeListComponent {
public themes:Theme[];
constructor(private themeService:ThemeService,private routeParams:RouteParams) {
let id = +this.routeParams.get('id');
this.themeService.getThemes(id).subscribe((themes:Theme[])=>{
this.themes = themes;
});
}
}
每当我在孩子的构造函数中实现 routerParams 时,都会出现错误。
【问题讨论】:
标签: typescript angular angular2-routing