Error TS2554: Expected 2 arguments, but got 1 using @ViewChild

Dung Do Tien Nov 02 2020 56

I want to call a method of child component from parent com. I created a ViewChild variable in the parent component as below:

@ViewChild(PromotionComponent) promotionChild: PromotionComponent;
@ViewChild(ChartTabComponent) chartChild: ChartTabComponent;
@ViewChild(PaidupComponent) paidupChild: PaidupComponent;

But after building the code, I got some errors  error TS2554: Expected 2 arguments, but got 1. full error as below:

 ERROR in src/app/report/pages/bump-chart/bump-chart.component.ts(20,4): error TS2554: Expected 2 arguments, but got 1.
  src/app/report/pages/bump-chart/bump-chart.component.ts(21,4): error TS2554: Expected 2 arguments, but got 1.
  src/app/report/pages/bump-chart/bump-chart.component.ts(22,4): error TS2554: Expected 2 arguments, but got 1.

I'm using Angular version 8.0, I just test it in version 7.0 it's still working for me.

Thanks for any suggestions.

Have 1 answer(s) found.
  • From angular 8 to above, you have to determine that the child component is static or not:

    @ViewChild(ChildComponentClass, {static: true|false}) variableViewChild; 

    So, you can change your code as below:

    @ViewChild(PromotionComponent, { static: true }) promotionChild: PromotionComponent;
    @ViewChild(ChartTabComponent, { static: true }) chartChild: ChartTabComponent;
    @ViewChild(PaidupComponent, { static: true }) paidupChild: PaidupComponent; 

    I hope this answer will helpful to you.

    Sandeep Kumar Nov 02 2020
Leave An Answer
* NOTE: You need Login before leave an answer

* Type maximum 2000 characters.

* All comments have to wait approved before display.

* Please polite comment and respect questions and answers of others.

Popular Tips