错误 TS2554:需要 2 个参数,但使用 @ViewChild 得到 1 个 [英] error TS2554: Expected 2 arguments, but got 1 with @ViewChild

查看:45
本文介绍了错误 TS2554:需要 2 个参数,但使用 @ViewChild 得到 1 个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 ViewChild 如下:

I was using ViewChild as follows:

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

在 angular-7.x 之前一直运行良好

Which was working fine till angular-7.x

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

as soon as I upgraded it to angular-8.x it started giving following error

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

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

I checked https://angular.io/api/core/ViewChild and when I change it to

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

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

It works. I'm not getting what static does and what should be it's value to work as previous?

推荐答案

根据 Angular 文档进行静态检查

According to the Angular documentation static checks

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

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,则查询将在任何更改检测后运行.如果设置为 true,它将立即运行.

Effectively this determines when the query is run to retrieve the element. If set to false the query will be run after any change detections. If set to true it will be run immediately.

有关更多信息以及包含此选项的原因,请参阅此 Github 问题.

For more information and why this option is included please see this Github issue.

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

The behavior you are probably looking for is to set static to false. This will result in the old behavior. However if your component's view is not dynamic (for example you do not use *ngIf) you should be able to set it to true safely.

这篇关于错误 TS2554:需要 2 个参数,但使用 @ViewChild 得到 1 个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆