이 라우팅이 있습니다.
{ path: 'item/:id',
component: parentComponent,
children: [
{ path: 'edit', component: childComponent }
]
}
이 위치로 이동하려면 다음을 사용합니다.
this.router.navigate(['/item/' + item.id + '/edit']);
이제 내 질문은 다음과 같습니다.
이제 내 childComponent가 ID를 어떻게 알 수 있습니까? child.component.ts에서 ID를 얻으려면 어떤 코드가 필요합니까? (parentComponent도 ID를 알아야하므로 끝에 매개 변수가 없습니다)
하위 구성 요소에서 다음 줄을 추가 할 수 있습니다.
현재 스냅 샷이 하위 구성 요소 인 현재 경로이므로 상위 경로 스냅 샷을 대상으로 지정해야합니다.
constructor(private route: ActivatedRoute) {
this.id = this.route.parent.snapshot.paramMap.get('id')
console.log('got the id', this.id);
}
또는 하위 구성 요소의 매개 변수를 구독하려는 경우 대신 사용할 수 있습니다.
constructor(private route: ActivatedRoute) {
this.route.parent.params.subscribe(params => console.log('params id', params.id));
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다