Use Directive to get its ViewContainerRef instead of using @ViewChild
https://hackernoon.com/here-is-how-to-get-viewcontainerref-before-viewchild-query-is-evaluated-f649e51315fb
https://hackernoon.com/here-is-how-to-get-viewcontainerref-before-viewchild-query-is-evaluated-f649e51315fb
{ static: true } needs to be set when you want to access the ViewChild in ngOnInit. { static: false } can only be accessed in ngAfterViewInit. This is also what you want to go for…
所支持的选择器包括: 任何带有 @Component 或 @Directive 装饰器的类 字符串形式的模板引用变量(比如可以使用 @ViewChild(‘cmp’) 来查询 <my-component #cmp></my-component> 组件树中任何当前组件的子组件所定义的提供商(比如 @ViewChild(SomeService) someService: SomeService ) 任何通过字符串令牌定义的提供商(比如 @ViewChild(‘someToken’) someTokenVal: any ) TemplateRef(比如可以用 @ViewChild(TemplateRef) template; 来查询 <ng-template></ng-template>) @ViewChild(‘myname’, {read: ViewContainerRef}) target : ViewContainerRef; The following is an explanation to read: There can be several instances…