error TS2554: Expected 2 arguments, but got 1 with @ViewChild

57,416

Solution 1

According to the Angular documentation static checks

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.

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.

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

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.

Solution 2

After migration to Angular 8 you should declare manually if it's static or not

@ViewChild(QuilldEditorComponent, {static: true}) quillEditorComponentInstance;

If you have further questions ask them or for more details please read this issue https://github.com/angular/angular-cli/issues/14553 or take a look at offical documentation https://angular.io/guide/static-query-migration

// query results available in ngOnInit
@ViewChild('foo', {static: true}) foo: ElementRef; 

OR

// query results available in ngAfterViewInit
@ViewChild('foo', {static: false}) foo: ElementRef;

Share:
57,416
Akhilesh Kumar
Author by

Akhilesh Kumar

Client-Side Technology Base: HTML5, CSS3, JavaScript (ES 5, 6+), TypeScript, Web Components, Browser Extensions CSS Frameworks / Preprocessor: Twitter Bootstrap, Skeleton, Spectre, material design, Less, Sass, PostCSS JS Frameworks / Library: Angular (1.x, 2, 4, 6, 7, 8, 9), reactJS, redux, vueJS, Hyperapp, Polymer, Knockout, jQuary, Require.js, Underscore.js, Turn.js, High Chart, Am Chart, Zing Chart, Chart.js, Google chart, immutableJS Task Runner: Grunt, Gulp, Webpack App Development: Android app development, Hybrid App development, Cordova, Cordova Plugin Development, React Native, React Native Plugin Development, Ionic, Phonegap Design Paradigm: MVC, MVVC, Micro-UI, Constructor Pattern, PubSub, Flux, V+logic Server-Side Technology: Base: NodeJS, Java, JSP, php, C++, Shell Scripting, Python, Go, Apache Shindig Frameworks / Library: Express, Async, hypernova, handlebar, pug, jade, line-by-line, json-schema, Passport, Database Connectors Database: MySQL, ORACLE 10g/11g, MongoDB, PostgreSQL, Elastic-Search, noe4j, Informix Utility: Clustered Server, Load Balancer, AWS (EC2, Cloud9, S3, Lambda, Serverless), Docker, Jenkins, C-Panel, Apache, Apache Tomcat, Apache Maven. Gradle Unit/Integrated Dev Test: Mocha, chaiJS, Should, Assert, expect, Mocky, Grunt-cefe-mocha, request, Protractor, Jest Others: WordPress, Optimize-Press, HTML-email, VOS, VOS Kernel programming, VOS Media Micro-Services, Google API (Translation, GCP, Map etc.), IOT IDEs / Servers / Tools: Eclipse, Webstorm, PyCharm, Android Studio, XCode, Visual Studio Version Control Systems: Tortoise SVN Client, Git, GitHub, Bitbucket, GitLab, Git API Big-Data: Data Analysis, Data Cleansing, Data Optimization, Data Processing, Data Visualization Data Processing Tool: Sicence, PowerBI, Data-Cleaner Web Security: Website Security Analyst, Cryptography, Back-end protection Game Engine and Logic: Smart-Fox Multiplayer Game Server, Pot Logic, Game Logic, Score Management Embedded System: Raspberry pi 3, wiring-pi, Microcontroller/Raspberry Pi connection through Web-Interface SDLC worked upon: Agile Methodology, Waterfall, Build & Fix Web-App Approach: PWA, Atomic Design, MVC, MVA

Updated on June 26, 2020

Comments

  • Akhilesh Kumar
    Akhilesh Kumar almost 4 years

    I was using ViewChild as follows:

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

    Which was working fine till angular-7.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.
    

    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?

  • ps0604
    ps0604 almost 5 years
    Didn't they say that after Angular 2 everything will be backward compatible?
  • Mathyn
    Mathyn almost 5 years
    @ps0604 Not as far as I know. This page in the documentation even talks about breaking changes and how they will handle them.