错误 TS2554:需要 2 个参数,但使用 @ViewChild 得到 1 个

error TS2554: Expected 2 arguments, but got 1 with @ViewChild

我按如下方式使用 ViewChild:

@ViewChild("InternalMedia") localStream;
@ViewChild("emoji") mEmoji;

在 angular-7.x

之前工作正常

我将其升级到 angular-8.x 后,它开始出现以下错误

.../call_emoji/component.ts(41,4): error TS2554: Expected 2 arguments, but got 1.

我检查了 https://angular.io/api/core/ViewChild,当我将其更改为

@ViewChild("InternalMedia",{static:false}) remoteStream;

有效。我不明白 static 的作用以及像以前一样工作的价值是什么?

根据 Angular 文档静态检查

whether or not to resolve query results before change detection runs (i.e. return static results only). If this option is not provided, the compiler will fall back to its default behavior, which is to use query results to determine the timing of query resolution. If any query results are inside a nested view (e.g. *ngIf), the query will be resolved after change detection runs. Otherwise, it will be resolved before change detection runs.

有效地确定查询何时 运行 检索元素。如果设置为 false,查询将在任何更改检测之后为 运行。如果设置为真,它将立即 运行。

有关详细信息以及包含此选项的原因,请参阅 this Github issue

您可能正在寻找的行为是将 static 设置为 false。这将导致旧行为。但是,如果您的组件的视图不是动态的(例如您不使用 *ngIf),您应该能够安全地将其设置为 true。

迁移到 Angular 8 后,您应该手动声明它是否是静态的

@ViewChild(QuilldEditorComponent, {static: true}) quillEditorComponentInstance;

如果您有进一步的问题向他们提问或了解更多详情,请阅读本期 https://github.com/angular/angular-cli/issues/14553 或者看看官方文档 https://angular.io/guide/static-query-migration

// query results available in ngOnInit
@ViewChild('foo', {static: true}) foo: ElementRef; 

OR

// query results available in ngAfterViewInit
@ViewChild('foo', {static: false}) foo: ElementRef;