我正在使用angularfire2模块。我创建了FireQueryModule,它调用了angularfire2模块。FireQueryModule 我已经注入到我的 AppModule 中。
流:应用模块 -> 火查询模块 -> 角度火2
如果我在我的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
}
我没有到达它破裂的地方。
同样的问题,如何解决:
我的库模块 :
@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
]
}
}
}