问题
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();
回答1:
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();
来源:https://stackoverflow.com/questions/57990675/getting-error-ts2554-expected-1-arguments-but-got-0-when-calling-a-construc