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

天涯浪子 提交于 2020-01-06 08:09:11

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!