繁体   English   中英

访问子路由:id

[英]Get access to child route :id

我有一个子组件及其父组件。 子组件包含以下管道来订阅激活的路由:

this.route.paramMap.pipe(
  map(paramMap => +paramMap.get('id')),
  switchMap((id: number) => this.apiService.getTasks(id.toString())),
).subscribe(tasks => this.tasks = tasks);

{ path: '', component: DashboardComponent, children: [
      { path: '', redirectTo: '0', pathMatch: 'full' },
      { path: '0', component: NoListComponent },
      { path: ':id', component: ListComponent }
    ]},

如何在父组件DashboardComponent访问此路由 :id ?

我问是因为我需要在父级内部使用相同的 ID。 我已经尝试通过@ViewChild传递它,但这不会在路线更改时更新。

编辑:事件发射是不可能的,因为我使用的是路由器插座。

通过router-outlet的父子组件通信是通过修改Parent组件的onActivate函数来实现的,如下所示:

onActivate(childComponent) {
    childComponent.someEventEmitter.subscribe((data) => {
    // Will receive the data from child here 
})

Child 组件像往常一样创建 EventEmitter:

export class ChildComponent {
  @Output() someEventEmitter: EventEmitter<any> = new EventEmitter();

  someFunction(data) {
    // emit data to parent component
    this.someEventEmitter.emit(data);
  }
}

在 html 中:

<button type="button" (click)="someFunction()">Do Something</button>

暂无
暂无

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

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