2016-04-25 5 views
1

ich den neuen Rahmen Angular2 von Google durch diese Videos Tutorials auf Youtube Lernen: https://www.youtube.com/watch?v=SEM6-TkuOgo„... dekoriert mit injizierbaren“ - Angular2

begegne ich andere Art von Fehler, wenn ich über Dienstleistungen zu lernen begann, wie :

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.(…)

ich verstehe nicht, was die Konsole wirklich bedeutet, habe ich versucht, einen Teil des Codes zu kommentieren, wenn oder nicht meinen Code Kompilierung zu überprüfen, zu testen.

Wenn das Gefühl, dass das Problem, um diesen Teil in dem Kontakt-list.component.ts sein könnte:

constructor(private _contactService: ContactService) {} 

Und auch dieses in dem contact.service.ts:

return Promise.resolve(CONTACTS); 

Ich machte einen Screenshot der verschiedenen TypeScript-Dateien im Projekt: http://imgur.com/a/mqb5P

Wenn jemand etwas Ahnung hat, diesen Code zu debuggen, bin ich ganz Ohr! :)

PS: Ich kann den Code aus dem Screenshot kopieren/einfügen, wenn dies helfen könnte.

Antwort

0

Die meiste Zeit ist dieses Problem, weil der Import für den Typ, der in den Konstruktorparametern angegeben wird, Mission oder falsch ist.

Importieren Sie den Typ "ContactService" in Ihre Datei component.list.ts?

Etwas wie folgt aus:

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

Angular2 verantwortlich ist, welche Arten von Instanzen, um herauszufinden, Sie in den Konstruktor injizieren wollen. Bei TypeScript werden die von Ihnen angegebenen Parametertypen verwendet, sie müssen jedoch zuvor in Ihr Modul importiert werden.

Die Abhängigkeitsinjektion erfolgt mithilfe eines Dekorators: @Injectable für einfache Klassen und @Component/@Directive für Komponenten/Direktiven. Aber du brauchst einen Dekorateur.

+0

Ja ich verifiziert I importiert alle benötigten Dateien überprüft auch zweimal den Pfad zum Repository. Ich habe einen Plunker gemacht, damit es einfacher wird, den Code zu überprüfen: http://plnkr.co/edit/TBeK15tcQlVSkAjRTPSJ – Dabolo

+0

Haben Sie das gleiche Problem, indem Sie Ihr '

+0

Keine Änderung mit oder ohne ''. – Dabolo

1

Wenn Sie eine Komponente ContactListComponent wie

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

und einen Service wie

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

dann die ContactService Klasse haben muss wie

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

Hallo Günter! Danke, aber selbst wenn ich meine contact.service.ts im Vergleich zu Ihrem Rat mit einem Konstruktor korrigiere, bekam ich folgendes:> angular2-polyfills.js: 332 Fehler: TypeError: Eigenschaft 'getOptional' von undefined (...) kann nicht gelesen werden machte einen Plunker, um den Code zu überprüfen: http: // plnkr.co/edit/TBeK15tcQlVSkAjRTPSJ – Dabolo

+0

https://plnkr.co/edit/85g0L8?p=preview 'ngOnInit' hatte einen Tippfehler. Ich habe deinen Code auch in eine Plunker-Vorlage von mir verschoben. –

+0

Woah genau! Aber selbst wenn ich meinen Fehler beim Schreiben von ngOnInit richtig korrigiere, habe ich eine Reihe von Fehlern: http://i.imgur.com/vTPdftA.png – Dabolo

Verwandte Themen