【问题标题】:Getting routerParams send from parent with child获取从父母与孩子一起发送的routerParams
【发布时间】: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


【解决方案1】:

我遇到了类似的问题。我通过简单地将 RouteParams 和 Router 与 RouteConfig、ROUTER_DIRECTIVES、RouterOutlet 导入同一行来解决它。

这可能会给你一个错误。所以我改变了

import {RouteConfig,ROUTER_DIRECTIVES, RouterOutlet} from 'angular2/router';
import {Router,RouteParams} from "angular2/router";

import {RouteConfig,ROUTER_DIRECTIVES, RouterOutlet, Router,RouteParams} from 'angular2/router';

不知何故,我不明白为什么这不适用于两个单独的导入,但这应该可以解决问题。

【讨论】:

  • 这是一个已知问题。对导入使用不同的引号 '" 是行不通的,但这是个好主意。
【解决方案2】:

目前没有简单的方法。另见https://github.com/angular/angular/issues/6985

Angular 2: getting RouteParams from parent component 提供了一些解决方法。

【讨论】:

  • 我尝试使用注射器解决方案,但一旦我使用get.(RouteParams),它就会出现同样的错误。
  • 对不起,没有想法。你能创建一个 Plunker。这将使调试更容易。
  • 我认为不可能在您声明@RouteConfig 的同一组件中使用RouteParams。我想我会使用另一个丑陋的解决方案,将我需要的参数保存在服务中,然后在需要时询问它
  • 目前只有丑陋的解决方法。你自己决定哪一个最不丑:-/
猜你喜欢
  • 2015-09-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-08-03
  • 2011-05-23
  • 1970-01-01
相关资源
最近更新 更多