简体   繁体   English

Angular 2 - 什么是DI错误?

[英]Angular 2 - What is a DI error?

I'm having an error when I import a service, but I have no idea why 我导入服务时出错,但我不明白为什么

import { CDService } from "../../services/definition";

constructor(
    private toastyCommunicationService: ToastyCommunicationService,
    private CDService : CDService,
    private SharedService: SharedService,
    private DatePipe: DatePipe
) { }

I have made my service litteraly empty because I was so frustrated with the error and not knowing what was causing it. 我让我的服务很空洞,因为我对错误感到非常沮丧,不知道是什么导致了它。

export class CDService {

}

Now I receive the error 现在我收到了错误

EXCEPTION: Uncaught (in promise): Error: DI Error
Error: DI Error

I have 2 questions: 我有两个问题:

1) What the hell is a DI error? 1) DI错误到底是什么?

2) How can I solve this error? 2)我该如何解决这个错误?

It is dependency injection error. 这是依赖注入错误。 I think you are missing @Injectable() decorator: 我认为你缺少@Injectable()装饰器:

@Injectable() // this line
export class CDService {

} 

Edit: 编辑:

Have you added CDService into NgModule? 你有没有将CDService添加到NgModule中?

确保将此服务添加到模块的providers

Make sure your service is injectable by using @Injectable() . 使用@Injectable()确保您的服务是可@Injectable() This way angular understands its not only a typesscript class but its a angular service. 这种方式角度不仅可以理解它的类型脚本,还可以理解它的角度服务。

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

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