简体   繁体   中英

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?

2) How can I solve this error?

It is dependency injection error. I think you are missing @Injectable() decorator:

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

} 

Edit:

Have you added CDService into NgModule?

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

Make sure your service is injectable by using @Injectable() . This way angular understands its not only a typesscript class but its a angular service.

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