【问题标题】:Angular injection error AngularFirestore -> InjectionToken Platform ID`Angular 注入错误 AngularFirestore -> InjectionToken Platform ID`
【发布时间】:2018-09-24 22:15:33
【问题描述】:

我正在使用 angularfire2 模块。我创建了调用 angularfire2 模块的 FireQueryModule。 FireQueryModule 我已经注入了我的 AppModule。

流程:AppModule -> FireQueryModule -> angularfire2

如果我在 FireQueryService 中使用 AngularFirestore 服务,我会遇到错误

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
}

我不明白它的坏处。

【问题讨论】:

  • 您似乎没有将@angular/platform-* 中的BrowserModuleServerModule 导入到您的模块中,您可以尝试这样做,看看它是否会改变错误?
  • @JamesDaniels 谢谢,它正在工作,但现在我收到另一个错误NullInjectorError: No provider for Console!
  • @RahulSharma 我遇到了与No provider for Console! 相同的问题你有没有弄清楚是什么原因造成的?
  • 同样的错误,关于信息你不能在自定义库模块中导入 BrowserModule

标签: angular firebase angularfire2


【解决方案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]
    };
  }
}

我的库包.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
      ]
    }
  }
}

【讨论】:

  • 那么解决方案是什么?尝试相同的方法,但对我不起作用。顺便问一下,您如何使用该嵌套库的模块?
猜你喜欢
  • 1970-01-01
  • 2020-05-19
  • 1970-01-01
  • 1970-01-01
  • 2019-07-09
  • 1970-01-01
  • 2021-04-23
  • 2019-04-18
  • 2015-09-11
相关资源
最近更新 更多