简体   繁体   English

在单击的组件的路由器插座中渲染路由

[英]Rendering route within clicked component's router-outlet

I have a list of items and I would like to allow the user to click on the item and display more information below it.我有一个项目列表,我希望允许用户单击该项目并在其下方显示更多信息。 In order to do this I put a router outlet within each item component and assigned the relevant path to the routing module.为了做到这一点,我在每个项目组件中放置了一个路由器插座,并将相关路径分配给路由模块。 When clicked, however, instead of rendering the information in the clicked component it places the information in the last item component's router-outlet.但是,当单击时,它不会在单击的组件中呈现信息,而是将信息放在最后一个项目组件的路由器插座中。 Is there a way to render the data only within the clicked component?有没有办法只在单击的组件内呈现数据?

routing module:路由模块:

path: 'items', component: ItemListComponent,
  children:[
    {
      path: 'detail/:item_id', component: ItemDetailComponent 
    }

itemDetailComponent.html itemDetailComponent.html

<a [routerLink]="['detail', item.id]">{{item.name}}</a>

<router-outlet></router-outlet>

Here is an illustration of the functionality I want:这是我想要的功能的说明:

多个路由器插座

Although I wouldn't recommend implementing it using routing when inner component is only shown on click, you can have it done using the following approach:虽然我不建议在内部组件仅在单击时显示时使用路由来实现它,但您可以使用以下方法完成它:

Link interface链接接口

interface Link {
  path: [string, number];
  label: string;
}

Component零件

public links: Link[] = [
  {
    label: 'route 1',
    path: ["detail", 1],
  },
  {
    label: 'route 2',
    path: ["detail", 2],
  },
]

public activeLink: Link;

Template模板

<div *ngFor="let link of links" (click)="activeLink = link">

  <a [routerLink]="link.path">{{link.label}}</a>

  <router-outlet *ngIf="link == activeLink"></router-outlet>
  
</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM