경로 매개 변수의 변경에 응답하는 각도 2의 구성 요소가 있습니다 (주 라우트에서 벗어나지 않기 때문에 구성 요소가 처음부터 다시로드되지 않습니다. 구성 요소 코드 :. 이것은 치료와 _pageToShow
작동각도 2 - 경로 매개 변수의 변경에 대한 테스트
export class MyComponent{
ngOnInit() {
this._routeInfo.params.forEach((params: Params) => {
if (params['area']){
this._pageToShow =params['area'];
}
});
}
}
는
내가 경로의 변경 (관찰의 때문에 두 번째 트리거의 동작을 테스트하기 위해 노력하고있어 탐색에 적절한 설정되어 있지만 그건 나를 위해 일하는 것을 거부했습니다.) 내 시도는 다음과 같습니다.
it('sets PageToShow to new area if params.area is changed', fakeAsync(() => {
let routes : Params[] = [{ 'area': "Terry" }];
TestBed.overrideComponent(MyComponent, {
set: {
providers: [{ provide: ActivatedRoute,
useValue: { 'params': Observable.from(routes)}}]
}
});
let fixture = TestBed.createComponent(MyComponent);
let comp = fixture.componentInstance;
let route: ActivatedRoute = fixture.debugElement.injector.get(ActivatedRoute);
comp.ngOnInit();
expect(comp.PageToShow).toBe("Terry");
routes.splice(2,0,{ 'area': "Billy" });
fixture.detectChanges();
expect(comp.PageToShow).toBe("Billy");
}));
그러나 실행할 때 TypeError: Cannot read property 'subscribe' of undefined
예외가 발생합니다. fixture.detectChanges();
행없이 실행하면 두 번째 기대가 실패하여 실패합니다.
과 동기 동작을 강제로 '기능이 필요합니다 ! 나는 그것을 처음으로 완전히 놓쳤다. 해결책을 가져 주셔서 감사합니다. :) –
내부 링크에 대해서도'fragment'에도이 방법을 사용할 수있었습니다 :'fragment = new Subject();', 탭 내용 숨기기/표시에 적합합니다. 예를 들면. –