无法解析可注入服务的参数

Cant Resolve Parameters for injectable Service

我有一个 Angular2 项目 2.0.0,我在尝试加载该项目时在浏览器中收到以下错误:

我正在以相同的方式将客户服务注入到身份验证服务中,并且工作正常,但是当我尝试将身份验证服务注入到客户服务中时,出现了这个错误。

欢迎提出任何建议。

错误

metadata_resolver.js:508Uncaught 错误:无法解析 CustomerService 的所有参数:(AngularFireDatabase, ?, Token_FirebaseApp).(…)CompileMetadataResolver.getDependenciesMetadata @ metadata_resolver.js: 508CompileMetadataResolver.getTypeMetadata@metadata_resolver.js:405(匿名函数)@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_._bootstrapModuleWithZone@application_ref.js:303PlatformRef_.bootstrapModule @ application_ref.js:285(匿名函数) @main.ts:12__webpack_require__ @bootstrap 138b37d…:52(匿名函数) @zone.js:1327__webpack_require__ @ bootstrap 138b37d…:52webpackJsonpCallback @bootstrap 138b37d…:23(匿名函数) @main.bundle.js:1

客服

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();
    }

}

身份验证服务

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();
  }
}

应用模块

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 { }

尝试在您的 CustomerService 构造函数中添加 @Inject(forwardRef(() => AuthenticationService)) private authService: AuthenticationService

记得从`@angular/core导入forwardRef