错误:
error TS2554: Expected 2 arguments, but got 1.
应有 2 个参数,但获得 1 个。ts(2554)
core.d.ts(7888, 47): An argument for 'opts' was not provided.
原因:ViewChild需要两个参数,并没有提供opts
官网解释:
static - 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.
此段解释在中文文档中并没有被翻译,大体意思如下:
static - 是否在更改检测运行之前解析查询结果(即只返回静态结果)。如果不提供此选项,编译器将退回到其默认行为,即使用查询结果来确定查询解析的时间。如果任何查询结果位于嵌套视图中(例如*ngIf),则在运行更改检测后将解析该查询。否则,它将在变更检测运行之前被解析。
解决:(按他说的添加参数就行了)
@ViewChild('child',{static:true}) child:any;
//或者
@ViewChild('child',{static:false}) child:any;