2016-04-25 56 views
1

我正在學習從谷歌的新框架Angular2在Youtube:https://www.youtube.com/watch?v=SEM6-TkuOgo「......飾以注射」 - 通過這些視頻教程Angular2

我遇到不同類型的錯誤,當我開始瞭解服務如:

EXCEPTION: Cannot resolve all parameters for 'ContactListComponent'(?). Make sure that all the parameters are decorated with Inject or have valid type annotations and that 'ContactListComponent' is decorated with Injectable.

EXCEPTION: Error: Uncaught (in promise): Cannot resolve all parameters for 'ContactListComponent'(?). Make sure that all the parameters are decorated with Inject or have valid type annotations and that 'ContactListComponent' is decorated with Injectable.

angular2-polyfills.js:469 Unhandled Promise rejection: Cannot resolve all parameters for 'ContactListComponent'(?). Make sure that all the parameters are decorated with Inject or have valid type annotations and that 'ContactListComponent' is decorated with Injectable. ; Zone: angular ; Task: Promise.then ; Value:

NoAnnotationError {message: "Cannot resolve all parameters for 'ContactListComp…ntactListComponent' is decorated with Injectable.", stack: "Error: Cannot resolve all parameters for 'ContactL…node_modules/angular2/bundles/angular2.js:477:94)"} message : "Cannot resolve all parameters for 'ContactListComponent'(?). Make sure that all the parameters are decorated with Inject or have valid type annotations and that 'ContactListComponent' is decorated with Injectable." stack : "Error: Cannot resolve all parameters for 'ContactListComponent'(?). Make sure that all the parameters are decorated with Inject or have valid type annotations and that 'ContactListComponent' is decorated/

angular2-polyfills.js:471 Error: Uncaught (in promise): Cannot resolve all parameters for 'ContactListComponent'(?). Make sure that all the parameters are decorated with Inject or have valid type annotations and that 'ContactListComponent' is decorated with Injectable.(…)

angular2-polyfills.js:469 Unhandled Promise rejection: Cannot resolve all parameters for 'ContactListComponent'(?). Make sure that all the parameters are decorated with Inject or have valid type annotations and that 'ContactListComponent' is decorated with Injectable. ; Zone: ; Task: Promise.then ; Value: NoAnnotationError {message: "Cannot resolve all parameters for 'ContactListComp…ntactListComponent' is decorated with Injectable.", stack: "Error: Cannot resolve all parameters for 'ContactL…node_modules/angular2/bundles/angular2.js:477:94)"}

angular2-polyfills.js:471 Error: Uncaught (in promise): Cannot resolve all parameters for 'ContactListComponent'(?). Make sure that all the parameters are decorated with Inject or have valid type annotations and that 'ContactListComponent' is decorated with Injectable.(…)

我不明白是什麼控制檯的真正含義我想註釋代碼的某些部分進行測試,以檢查時,我的代碼編譯或沒有。

如果覺得這個問題可能是解決這個部分在接觸list.component.ts:

constructor(private _contactService: ContactService) {} 

而這也是一個在contact.service.ts:

return Promise.resolve(CONTACTS); 

我截取了該項目中不同TypeScript文件的屏幕截圖:http://imgur.com/a/mqb5P

如果有人對調試此代碼有一些線索,我非常感謝! :)

PS:如果這可能有幫助,我可以從屏幕截圖複製/粘貼代碼。

回答

0

這種問題的大部分時間是因爲在構造函數參數中指定的類型的導入是任務或錯誤。

您是否在component.list.ts文件中導入了「ContactService」類型?

事情是這樣的:

import { ContactService } from './contact.service'; 

Angular2負責找出哪些類型要注入到構造函數實例。使用TypeScript,它使用您指定的參數類型完成,但它們需要先導入到模塊中。

依賴注入是使用裝飾器完成的:@Injectable用於簡單類,@Component/@Directive用於組件/指令。但你需要一個裝飾者。

+0

是的,我確認我導入所需的所有文件,同時檢查兩次到存儲庫的路徑。我做了一個Plunker,所以這可能會更容易檢查代碼:http://plnkr.co/edit/TBeK15tcQlVSkAjRTPSJ – Dabolo

+0

你是否有同樣的問題,通過刪除你的索引中的

+0

無論有無'都沒有變化。 – Dabolo

1

如果你有一個像

export class ContactListComponent { 
    constructor(private contactService:ContactService) {} 
} 

組件ContactListComponent和像

export class ContactService { 
    constructor(private someDep:SomeDependency) {} 
} 

那麼的ContactService類需要的服務有一個@Injectable()裝飾像

@Injectable() 
export class ContactService { 
    constructor(private someDep:SomeDependency) {} 
} 
+0

嗨Günter!謝謝,但即使我修復我的contact.service.ts相比,你的建議與構造函數我得到這個:> angular2-polyfills.js:332錯誤:TypeError:無法讀取屬性'getOptional'未定義(...)所以我做了一個Plunker來幫助檢查代碼:http:// plnkr。co/edit/TBeK15tcQlVSkAjRTPSJ – Dabolo

+0

https://plnkr.co/edit/85g0L8?p=preview'ngOnInit'有一個錯字。我也將你的代碼移動到我的Plunker模板中。 –

+0

哇!但即使當我糾正我的錯誤寫ngOnInit的權利,我有一堆的錯誤:http://i.imgur.com/vTPdftA.png – Dabolo