简体   繁体   English

调用构造函数时出现“错误TS2554:预期有1个参数,但有0。”

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

Getting an error 遇到错误

error TS2554: Expected 1 arguments, but got 0. 错误TS2554:预期有1个参数,但得到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. Angular自动解决组件和服务的依赖关系。 However, when you call your class like that: 但是,当您像这样调用课程时:

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

Then you need to supply a dependency BackendErrorsService . 然后,您需要提供依赖项BackendErrorsService Something like that: 像这样:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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