Angular 2访问来自子组件的父路由队列

时间:2016-01-20 17:41:15

标签: javascript routing angular angular-routing

我有这个(主要的父母)组件 -

@RouteConfig([
    { path: '/', name: 'ProjectList', component: ProjectListComponent, useAsDefault: true },
    { path: '/new', name: 'ProjectNew', component: ProjectFormComponent },
    { path: '/:id', name: 'ProjectDetail', component: ProjectDetailComponent },
    { path: '/:id/issues/...', name: 'Issue', component: IssueMountComponent },
])
class ProjectMountComponent {
}

然后我得到了第二个装载组件(主要父级的子级,下一个组件的父级)

@Component({
        template: `
        <div><router-outlet></router-outlet></div>
    `,
        directives: [RouterLink, RouterOutlet]
})
@RouteConfig([
        { path: "/", name: "IssueList", component: IssueListComponent, useAsDefault: true },
])
class IssueMountComponent {
    constructor(private _routeParams: RouteParams) {
    }
}

在这里,我可以毫无问题地访问routeParams(:id)。现在,这里是我需要来自uri的:id值的组件。

@Component({
        template: `
        <h3>Issue List</h3>
        <ul>
            <issue-component *ngFor="#issue of issues" [issue]="issue"></issue-component>
        </ul>
    `,
        directives: [IssueComponent],
        providers: [IssueService]
})
class IssueListComponent implements OnInit {
        issues: Issue[];
        constructor(private _issueService: IssueService,
                                private _routeParams: RouteParams) {}

        getIssues() {
            let id = this._routeParams.get('id');
            console.log(this._routeParams);
            this._issueService.getIssues(id).then(issues => this.issues = issues);
        }

        ngOnInit() {
            this.getIssues();
        }
}

在此组件中,我无法访问:id路由参数的值。它始终为空。我做错了什么?

这是组件层次结构 -

ProjectMountComponent - &gt; IssueMountComponent - &gt; IssueListComponent

0 个答案:

没有答案