我有两个组件,列表和详细视图 . 单击ListComponent上的项目时,我想将当前数据集交给Detailed组件 . 这背后的想法是,如果数据已经可用,则DetailedComponent不必通过服务重新获取它 .

是否可以向routerLink添加一个对象,其中的详细信息比获取onInit()?

例如 .

路由器

...
{ path: 'hero/:id', component: HeroDetailComponent }

ListComponent

onSelect(hero: Hero) {
  this.router.navigate(['/hero', hero.id]); // how to add hero as heroData
}

DetailComponent

ngOnInit() {
  if( this.route.params.heroData ) {
    this.hero = heroData // < this is what i like to do.
  } else {
    this.route.params
      // (+) converts string 'id' to a number
      .switchMap((params: Params) => this.service.getHero(+params['id']))
      .subscribe((hero: Hero) => this.hero = hero)
  }
}

我确实知道angular 2 custom route parameters但是它们会暴露所有数据并使其可操作,或者如果保留网址则使用旧数据 . 因此,这不是我想去的地方 .