我们正在使用
TestBed.overrideComponent(CoolComponent,{ set: { template: '<div id="fake-component">i am the fake component</div>',selector: 'our-cool-component',inputs: [ 'model' ] } })
覆盖组件.
该组件具有我们在ngOnInit方法中配置的ViewChild
@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依次生活在一个Wrapper组件中.
当我们在Wrapper fixture上调用fixture.detectChanges()时,会尝试构造CoolComponent,但是当它调用doStuff()时,它会立即死机,因为CoolChildComponent是未定义的.
有没有办法得到CoolComponent存根的CoolChildComponent?看起来我们可以把它从Wrapper中取消,因为它只能通过模板引用,而不是组件的属性.
ngOnInit() { this.coolChildComponent.doStuff(); }
应该
ngAfterViewInit() { this.coolChildComponent.doStuff(); }
View child is set before the ngAfterViewInit callback is called.