아래에 표시된 것처럼 경로 매개 변수에 대한 교과서를 상당히 등록하는 구성 요소가 있습니다.동일한 페이지의 두 구성 요소에서 경로 매개 변수에 액세스
export class tutorialComponent implements OnInit, OnDestroy {
private sub: Subscription;
public chapter: string;
constructor(private route: ActivatedRoute) {}
ngOnInit() {
this.sub = this.route.params.subscribe(params => {
this.chapter = params['id'];
});
}
ngOnDestroy() {
this.sub.unsubscribe();
}
}
그러나, 노선에 : 나는 브라우저에서 ng.probe($0).componentInstance
을 실행하면 (튜토리얼 CMP를 선택하지) http://localhost:3000/tutorial/chapter/0
, 그때 장을 볼 수 = 정의 - 왜?
또한 this.chapter = +params['id'];
을 사용하는 경우 chapter = NaN?
업데이트 :
나는 두 가지 구성 요소, 부모 (자습서)와 자식 (장) 구성 요소가 있습니다. 모두 동일 있습니다
ngOnInit() {
this.sub = this.route.params.subscribe(params => {
this.chapter = params['id'];
});
}
그러나, 하위 구성 요소가 올바르게 chapter
을 표시?
this.validationCode = route.snapshot.params['id'];
...
그러나 업데이트하고 싶습니다 ... –
나는 이해하지 못합니까? –
@JohnBaird - URL이 동적 인 것을 의미합니다. 옵저버가 필요합니다. – mrgoos