简体   繁体   中英

Getting “error TS2554: Expected 1 arguments, but got 0.” when calling a constructor

Getting an error

error TS2554: Expected 1 arguments, but got 0.

when the class instance is called. How can I fix this?

class ErrorHandler {

    constructor(private errorService: BackendErrorsService) {}
    getError() {
        console.log('error called');
    }
}

const instance = new ErrorHandler().getError();

Angular automatically resolve dependencies of components and services. However, when you call your class like that:

const instance = new ErrorHandler().getError();

Then you need to supply a dependency BackendErrorsService . Something like that:

let backendErrorsService = new BackendErrorsService();
const instance = new ErrorHandler(backendErrorsService ).getError();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM