I had another scenario with angular 9 that took me two days to figure out.
My problem was that none of the solutions worked; in fact, neither ViewChild nor ViewChildren worked in ANGULAR 9.
The issue, after A LOT of investigation and NO WARNS, was that my @ViewChild
was on an ABSTRACT CLASS that WASN'T marked as a @Component
.
So, if you're doing something like that:
export abstract class AbstractBaseComponent {
@ViewChild('test') public testRef: TestComponent;
}
@Component({
'selector': 'base-component'
})
export class BaseComponent extends AbstractBaseComponent {
ngAfterViewInit() {
console.log(this.testRef);
}
}
THIS WON'T WORK.
you must add @Component
on the abstract class as well:
@Component({})
export abstract class AbstractBaseComponent {
@ViewChild('test') public testRef: TestComponent;
}
Further reading about that can be (indirectly) found in the breaking changes in angular 9:
Although it's not mentioned that abstract classes follows the same behavior, it is actually intended to be as it is, since abstract classes are technically not transpiled.