Angular 8.x使用ViewChild報錯

錯誤:

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;  

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章