angular - Modificarea unui component definit în TestBed.overrideComponent
Noi folosim
TestBed.overrideComponent(CoolComponent, {
set: {
template: '<div id="fake-component">i am the fake component</div>',
selector: 'our-cool-component',
inputs: [ 'model' ]
}
})
a suprascrie un component.
Componenta are un ViewChild pe care îl configurăm în metoda noastră ngOnInit
@Component({
selector: 'our-cool-component',
templateUrl: 'cool.component.html'
})
export class CoolComponent implements OnInit, OnDestroy {
@Input() model: SomeModel
@ViewChild(CoolChildComponent) coolChildComponent;
ngOnInit() {
this.coolChildComponent.doStuff();
}
}
CoolComponent, la rândul său, se află într-un component Wrapper.
Când apelăm fixture.detectChanges() pe fixture-ul Wrapper, aceasta încearcă să construiască CoolComponent, dar se oprește imediat când apelează doStuff() pentru că CoolChildComponent este nedefinit.
Există o modalitate de a accesa CoolComponent pentru a înlocui cu un stub CoolChildComponent? Nu pare că putem să îl obținem de pe Wrapper deoarece este referit doar prin template, nu ca o proprietate a componentului.
Răspuns #1
ngOnInit() {
this.coolChildComponent.doStuff();
}
ar trebui să fie
ngAfterViewInit() {
this.coolChildComponent.doStuff();
}
pentru că
Vederea copilului este setată înainte ca funcția de apel înapoi ngAfterViewInit să fie apelată.
