这个错误通常发生在使用@Input访问ElementRef时,ElementRef没有被正确声明的时候。解决方案是在指令中注入ElementRef并将其作为构造函数的参数,就像这样:
import { Directive, ElementRef } from '@angular/core';
@Directive({ selector: '[myDirective]' }) export class MyDirective { constructor(private elementRef: ElementRef) { console.log(this.elementRef.nativeElement); } }
然后,在ng-container中使用这个指令:
这样就可以避免NG0100错误了。