2017-03-01 110 views
1

我有一個錯誤,當我導入服務,但我不知道爲什麼Angular 2 - 什麼是DI錯誤?

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

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

我已經作出了服務litteraly空的,因爲我是錯誤很沮喪,不知道是什麼導致它。

export class CDService { 

} 

現在我收到錯誤

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

我有2個問題:

1)到底是什麼DI錯誤?

2)我該如何解決這個錯誤?

+0

這是依賴注入錯誤。請發佈更多代碼。 –

+0

@IgorJanković哪個代碼?它需要哪些依賴關係?服務中沒有單行代碼 – Nicolas

回答

4

這是依賴注入錯誤。我認爲你缺少@Injectable()裝飾:

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

} 

編輯:

您已經添加到CDService NgModule?

+0

我更新了OP – Nicolas

+0

您是否已將CDService添加到NgModule中? –

+0

啊我完全忘了檢查!謝謝! – Nicolas

1

請確保您的服務通過使用@Injectable()注射。這種方式不僅能夠理解它的類型,而且是一種角度服務。

+0

還有其他方法,但以此開始是最好的方法 –

4

確保您已將此服務添加到您的模塊的providers