错误TS2554:预期有2个参数,但@ViewChild却有1个 [英] error TS2554: Expected 2 arguments, but got 1 with @ViewChild

查看:1163
本文介绍了错误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;

有效.我没有得到什么静态效果,并且像以前一样工作应该有什么价值?

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天全站免登陆