角度注入错误AngularFirestore - > InjectionToken平台ID`

时间:2018-06-01 16:54:54

标签: angular firebase angularfire2

我正在使用angularfire2模块。我创建了FireQueryModule,它调用了angularfire2模块。我在我的AppModule中注入了FireQueryModule。

流程: AppModule - > FireQueryModule - > angularfire2

如果我在FireQueryService中使用AngularFirestore Service,则会收到错误

AppComponent_Host.ngfactory.js? [sm]:1 ERROR Error: StaticInjectorError(AppModule)[AngularFirestore -> InjectionToken Platform ID]: 
  StaticInjectorError(Platform: core)[AngularFirestore -> InjectionToken Platform ID]: 
    NullInjectorError: No provider for InjectionToken Platform ID!
    at NullInjector.push../node_modules/@angular/core/fesm5/core.js.NullInjector.get (core.js:979)
    at resolveToken (core.js:1232)
    at tryResolveToken (core.js:1182)
    at StaticInjector.push../node_modules/@angular/core/fesm5/core.js.StaticInjector.get (core.js:1077)
    at resolveToken (core.js:1232)
    at tryResolveToken (core.js:1182)
    at StaticInjector.push../node_modules/@angular/core/fesm5/core.js.StaticInjector.get (core.js:1077)
    at resolveNgModuleDep (core.js:9217)
    at _createClass (core.js:9270)
    at _createProviderInstance$1 (core.js:9234)

模块代码

import { NgModule, ModuleWithProviders } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FireQueryService } from './services/firebase.service';
import { FirebaseAppConfig, AngularFireModule } from 'angularfire2';
import { AngularFirestoreModule } from 'angularfire2/firestore';
import { AngularFirestore } from 'angularfire2/firestore';

@NgModule({})
export class FireQueryModule {
  static forRoot(config: FirebaseAppConfig): ModuleWithProviders {
    @NgModule({
      imports: [
        CommonModule,
        AngularFirestoreModule,
        AngularFireModule.initializeApp(config)
      ],
      providers: [AngularFirestore]
    })
    class RootModule { }
    return {
      ngModule: RootModule,
      providers: [FireQueryService]
    };
  }
}

服务代码

@Injectable()
export class FireQueryService {
  constructor(private db: AngularFirestore) { } // error here if i comment it works
}

我没有得到它破碎的地方。

1 个答案:

答案 0 :(得分:1)

相同的问题,如何解决:

我的库模块:

@NgModule({})
export class DomainLibModule {
  static forRoot(config: FirebaseAppConfig): ModuleWithProviders {
    @NgModule({
      imports: [
        CommonModule,
        AngularFireDatabaseModule,
        AngularFireModule.initializeApp(config)
      ],
      providers: [AngularFireDatabase]
    })
    class RootModule { }
    return {
      ngModule: RootModule,
      providers: [DomainLibService]
    };
  }
}

我的Lib Package.json:

"peerDependencies": {
    "@angular/common": "6.x.x",
    "@angular/core": "6.x.x",
    "@angular/fire": "5.x.x",
    "@angular/platform-browser": "6.x.x",
    "@angular/platform-browser-dynamic": "6.x.x",
    "firebase": "5.x.x",
    "rxjs": "6.x.x"
  }

所以我所有的依赖都在我的应用程序上。我添加了平台浏览器和平台浏览器动力学,因为它们是AngularFire的依赖项(我不知道它是否重要)

最重要的可能是在此处,在我的应用程序tsconfig.json中

{
  "compilerOptions": {
    // ...
    "paths": {
      "@angular/*": [
        "./node_modules/@angular/*" // It could be an other path
      ]
    }
  }
}