2016-11-02 4 views
0

Ich habe ein Angular2 Projekt 2.0.0 und ich erhalte die folgende Fehlermeldung im Browser beim Versuch, das Projekt zu laden:Cant Resolve Parameter für injizierbare Dienst

ich den Kundendienstes in den Authentifizierungsdienst an Injektion in dem Derselbe Weg und es funktioniert gut, aber wenn ich versuche, den Authentifizierungsdienst in den Kundendienst zu injizieren, bekomme ich diesen Fehler.

Alle Vorschläge wären willkommen.

Fehler

metadata_resolver.js: 508Uncaught Fehler: nicht alle Parameter für Customer beheben. (? AngularFireDatabase, Token_FirebaseApp) (...) CompileMetadataResolver.getDependenciesMetadata @ metadata_resolver.js: 508CompileMetadataResolver.getTypeMetadata @ metadata_resolver.js: 405 (anonyme Funktion) @ metadata_resolver.js: 552CompileMetadataResolver.getProvidersMetadata @ metadata_resolver.js: 532CompileMetadataResolver.getNgModuleMetadata @ metadata_resolver.js: 285RuntimeCompiler._compileComponents @ runtime_compiler.js: 126RuntimeCompiler._compileModuleAndComponents @ runtime_compiler.js: 64RuntimeCompiler.compileModuleAsync @ runtime_compiler.js: 55PlatformRef _._ bootstrapModuleWithZon e @ application_ref.js: 303PlatformRef_.bootstrapModule @ application_ref.js: 285 (anonyme Funktion) @ main.ts: 12__webpack_require__ @ bootstrap 138b37d ...: 52 (anonyme Funktion) @ zone.js: 1327__webpack_require__ @ bootstrap 138b37d ...: 52webpackJsonpCallback @ bootstrap 138b37d ...: 23 (anonyme Funktion) @ main.bundle.js: 1

Kundenservice

import { Injectable, Inject } from '@angular/core'; 

import { Observable, Subject } from "rxjs/Rx"; 

import { FirebaseRef, AngularFireDatabase, FirebaseListObservable } from 'angularfire2'; 

import "rxjs/add/operator/filter"; 

import { Customer } from "./customer-model"; 
import { AuthenticationService } from '../authentication/authentication.service'; 

declare var firebase: any; 

@Injectable() 
export class CustomerService { 

    sdkDb: any; 
    customersRef: string = '/customers/'; 

    customer: Customer; 
    customers: FirebaseListObservable<Customer[]>; 

    constructor(
     private db: AngularFireDatabase, 
     private authService: AuthenticationService, 
     @Inject(FirebaseRef) fb 
    ) { 

     this.sdkDb = fb.database().ref(); 
    } 

    getCustomers(): Observable<Customer[]> { 

     return this.db.list(this.customersRef, { 
      query: { 
       orderByChild: 'organisation' 
      } 
     }) 
      .map(Customer.fromJsonList) 

    } 

    getCustomer(customerIndex: string) { 

     this.db.object('/customers/' + customerIndex) 
      .subscribe(customer => { 
       this.customer = customer; 
      }); 
     return this.customer; 
    } 

    addCustomer(customer: any) { 

     const newCustomer = Object.assign({}, customer); 

     const newCustomerKey = this.sdkDb.child(this.customersRef).push().key; 

     this.customer = newCustomerKey; 

     let dataToSave = {}; 

     dataToSave[this.customersRef + newCustomerKey] = newCustomer; 

     return this.firebaseUpdate(dataToSave); 

    } 

    updateCustomer(customerIndex: string, customer: Customer): Observable<any> { 

     const customertoSave = Object.assign({}, customer); 

     let dataToSave = {}; 
     dataToSave[this.customersRef + customerIndex] = customertoSave; 

     return this.firebaseUpdate(dataToSave); 

    } 

    deleteCustomer(customerIndex: string) { 
     this.db.object(this.customersRef + customerIndex).remove(); 
    } 

    firebaseUpdate(dataToSave) { 
     const subject = new Subject(); 

     this.sdkDb.update(dataToSave) 
      .then(
      val => { 
       subject.next(val); 
       subject.complete(); 

      }, 
      err => { 
       subject.error(err); 
       subject.complete(); 
      } 
      ); 

     return subject.asObservable(); 
    } 

} 

Authentication Service

import { Injectable, Inject } from "@angular/core"; 
import { Observable, Subject, BehaviorSubject } from 'rxjs/Rx'; 
import { FirebaseAuth, FirebaseAuthState, FirebaseRef, AngularFireDatabase } from 'angularfire2/index'; 

import { User, UserProfile } from "./user-model"; 
import { AuthInfo } from './auth-info'; 

import { Router } from "@angular/router"; 

import { Customer } from '../customer/customer-model'; 
import { CustomerService } from '../customer/customer.service'; 

declare var firebase: any; 

@Injectable() 
export class AuthenticationService { 
    user: User; 

    sdkDb: any; 
    customer: any; 
    usersRef: string = '/users/'; 
    customersRef: string = '/customers/'; 
    static UNKNOWN_USER = new AuthInfo(null); 
    authInfo$: BehaviorSubject<AuthInfo> = new BehaviorSubject<AuthInfo>(AuthenticationService.UNKNOWN_USER); 

    constructor(private auth: FirebaseAuth, 
    private db: AngularFireDatabase, 
    private customerService: CustomerService, 
    @Inject(FirebaseRef) fb, 
    private router: Router) { 
    this.sdkDb = fb.database().ref(); 
    } 

    getCurrentUserId() { 

    firebase.auth().onAuthStateChanged(function (user) { 
     if (user) { 
     this._userId = firebase.database().ref() + (firebase.auth().currentUser.uid); 
     return this._userid; 
     } 
    }); 
    } 

    signUpUser(user: User) { 
    this.user = user; 
    return this.fromFirebaseAuthPromise(this.auth.createUser(user) 
     .then(user => { 
     this.addNewCustomer(this.user.organisation); 
     this.addNewUserProfile(user.uid, this.customer); 
     })); 
    } 

    addNewCustomer(organisation: string) { 
    this.customer = new Object({ 
     organisation: '' 
     // overview: '', 
     // imagePath: '', 
    }); 
    this.customer.organisation = organisation; 

    return this.customerService.addCustomer(this.customer); 

    } 

    addNewUserProfile(userId: string, organisation: string) { 

    const newUserProfile = new UserProfile(); 
    newUserProfile.userId = userId; 
    newUserProfile.organisation = this.user.organisation; 

    const newUserProfileKey = this.sdkDb.child(this.usersRef).push().key; 

    let dataToSave = {}; 

    dataToSave[this.usersRef + newUserProfileKey] = newUserProfile; 

    this.firebaseUpdate(dataToSave); 

    } 

    signinUser(email, password): Observable<FirebaseAuthState> { 
    return this.fromFirebaseAuthPromise(this.auth.login({ email, password })); 
    } 

    fromFirebaseAuthPromise(promise): Observable<any> { 
    const subject = new Subject<any>(); 

    promise 
     .then(res => { 
     const authInfo = new AuthInfo(this.auth.getAuth().uid); 
     this.authInfo$.next(authInfo); 
     subject.next(res); 
     subject.complete(); 
     }, 
     err => { 
     this.authInfo$.error(err); 
     subject.error(err); 
     subject.complete(); 
     }); 

    return subject.asObservable(); 
    } 

    logout() { 
    firebase.auth().signOut(); 
    this.router.navigate(['/home']); 
    } 

    private postSignIn(): void { 
    this.router.navigate(['/customer/list']); 
    } 

    firebaseUpdate(dataToSave) { 
    const subject = new Subject(); 

    this.sdkDb.update(dataToSave) 
     .then(
     val => { 
     subject.next(val); 
     subject.complete(); 

     }, 
     err => { 
     subject.error(err); 
     subject.complete(); 
     } 
    ); 

    return subject.asObservable(); 
    } 
} 

App Modul

import { NgModule } from '@angular/core'; 
import { HttpModule, JsonpModule } from '@angular/http'; 
import { BrowserModule } from '@angular/platform-browser'; 
import { ReactiveFormsModule } from '@angular/forms'; 

import * as firebase from 'firebase'; 
import { AngularFireModule, AngularFire } from 'angularfire2'; 
import { firebaseConfig, authConfig } from '../environments/firebase.config'; 

import 'rxjs/add/operator/do'; 

import { MaterialModule } from '@angular/material'; 

import { AppComponent } from './app.component'; 

import { AppRoutingModule, routingComponents } from './app.routing'; 

import { AuthenticationModule } from "./authentication/authentication.module"; 
import { SharedModule } from "./shared/shared.module"; 
import { DashboardModule } from "./dashboard/dashboard.module"; 
import { CustomerModule } from "./customer/customer.module"; 
import { UserModule } from "./user/user.module"; 

import { AuthenticationService } from "./authentication/authentication.service"; 
import { AuthenticationGuard } from "./authentication/authentication.guard"; 
import { CustomerService } from "./customer/customer.service"; 
import { UserService } from "./user/user.service"; 
import { PagingService } from "./shared/paging/paging.service"; 
import { SearchService } from "./shared/search/search.service"; 

@NgModule({ 
    declarations: [ 
    AppComponent, 
    routingComponents 
    ], 
    imports: [ 
    BrowserModule, 
    ReactiveFormsModule, 
    HttpModule, 
    JsonpModule, 
    AngularFireModule.initializeApp(firebaseConfig, authConfig), 
    MaterialModule.forRoot(), 
    AppRoutingModule, 
    AuthenticationModule, 
    SharedModule, 
    DashboardModule, 
    CustomerModule, 
    UserModule 
    ], 
    providers: [ 
    AngularFire, 
    AuthenticationService, 
    AuthenticationGuard, 
    CustomerService, 
    UserService, 
    PagingService, 
    SearchService], 
    bootstrap: [AppComponent] 
}) 
export class AppModule { } 
+0

Es scheint, dass Sie hier eine zirkuläre Abhängigkeitsproblem haben. Versuchen Sie in Ihrem 'CustomerService' -Konstruktor' forwardRef (AuthenticationService) private authService: AuthenticationService' hinzuzufügen. –

+0

Danke - Es scheint, dass Sie richtig sind, ist es eine zirkuläre Abhängigkeit Problem, wenn ich den Kundenservice aus dem Authentifizierungsdienst nehmen, verschwindet der Fehler. Danke :-) Habe gerade etwas Neues über Angular2 gelernt. Ich bekomme jedoch einen Kompilierfehler damit im Konstruktor forwardRef (AuthenticationService) privaten authService: AuthenticationService, also änderte ich es in forwardRef = (AuthenticationService), private authService: AuthenticationService, aber ich bekomme die kann nicht alle Parameter wieder auflösen. Will etwas über zirkuläre Abhängigkeiten recherchieren. – ccocker

+0

Ich habe den Kommentar als Antwort hinzugefügt :) –

Antwort

1

Try @Inject(forwardRef(() => AuthenticationService)) private authService: AuthenticationService in Ihrem CustomerService Konstruktor hinzuzufügen.

Denken Sie daran, forwardRef von `@ angular/core zu importieren.

+0

Ja, ich tat dies, aber es hat einen Kompilierfehler es nicht ForwardRef (AuthenticationService) akzeptiert es sagt es erwartet forwardRef = (AuthenticationService). Dann sagt es, dass es ein "," erwartet, also endete ich mit forwardRef = (AuthenticationService), private authService: AuthenticationService. Aber dann kann ich immer noch keine Parameter auflösen. – ccocker

+0

Ich denke, ich muss meine Dienste umgestalten, so dass ich keine zirkuläre Abhängigkeit habe – ccocker

+0

@ccocker überprüfen Sie meine letzte Aktualisierung. Aber es ist besser, wenn Sie Ihre Dienste umgestalten können, um zirkuläre Abhängigkeiten zu vermeiden. –