繁体   English   中英

错误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;

有用。 我没有得到静态的东西,它应该像以前一样工作的价值是什么?

迁移到Angular 8后,如果它是静态的,则应手动声明

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

如果您有其他问题请询问他们或了解更多详情,请阅读此问题https://github.com/angular/angular-cli/issues/14553或查看官方文档https://angular.io/guide/static -query迁移

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

OR

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

根据Angular文档静态检查

是否在更改检测运行之前解析查询结果(即仅返回静态结果)。 如果未提供此选项,编译器将回退到其默认行为,即使用查询结果来确定查询解析的时间。 如果任何查询结果在嵌套视图中(例如* ngIf),则在更改检测运行后将解析查询。 否则,它将在更改检测运行之前解决。

实际上,这确定何时运行查询以检索元素。 如果设置为false,则将在任何更改检测后运行查询。 如果设置为true,它将立即运行。

有关更多信息以及为什么包含此选项,请参阅此Github问题

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM