我已将 AngularFire 添加到我的项目中。在其自述文件中,我被指示AngularFireModule.initializeApp(...)
将AppModule
然而,在示例中,人们使用环境常量,这不是一个选项,因为部署要求有一个配置文件,然后在部署时根据部署到的环境替换变量。
我相信我的配置是在导入之后加载的,这意味着没有任何东西传递给.initializeApp(...)
app.module.ts
import { AngularFireModule } from '@angular/fire';
import { AngularFireAuthModule } from '@angular/fire/auth';
...
export function initializeApp(appConfig: AppConfig) {
return () => appConfig.load();
}
@NgModule({
declarations: [
AppComponent
],
imports: [
...,
AngularFireModule.initializeApp(AppConfig.firebase),
AngularFireAuthModule
],
providers: [
AppConfig,
{ provide: APP_INITIALIZER, useFactory: initializeApp, deps: [AppConfig], multi: true },
...,
],
bootstrap: [AppComponent]
})
export class AppModule { }
应用配置.ts
import { Injectable } from '@angular/core';
import { AppConfigModel } from './shared/models/app-config.model';
import { HttpClient } from '@angular/common/http';
import { environment } from '../environments/environment';
/**
* This config pattern based off https://devblogs.microsoft.com/premier-developer/angular-how-to-editable-config-files/
* which is referenced in Octopus Deploy blog here https://octopus.com/blog/javascript-configuration
*/
@Injectable()
export class AppConfig extends AppConfigModel {
constructor(private http: HttpClient) {
super();
}
/**
* Loads the required config file from assets, depending on configName of environment.
* This allows replacement of variables by Octopus Deploy
*/
public load() {
const jsonFile = `assets/config/config.${environment.configName}.json`;
return new Promise<void>((resolve, reject) => {
this.http.get(jsonFile).toPromise().then((response: AppConfigModel) => {
this.mapConfigToProperties(response);
resolve();
}).catch((response: any) => {
console.error('On config load', response);
reject(`Could not config load file '${jsonFile}': ${JSON.stringify(response)}`);
});
});
}
private mapConfigToProperties(config: any) {
Object.keys(config).forEach(key => {
AppConfig[key] = config[key];
});
}
}
我非常想解决这个问题,而无需更改任何构建过程,并且只需能够使用AppConfig
来检索配置。
包.json
{
...,
"dependencies": {
"@angular/animations": "^6.1.10",
"@angular/cdk": "^6.3.0",
"@angular/common": "^6.0.3",
"@angular/compiler": "^6.0.3",
"@angular/core": "^6.0.3",
"@angular/fire": "^5.3.0",
"@angular/forms": "^6.0.3",
"@angular/http": "^6.0.3",
"@angular/platform-browser": "^6.0.3",
"@angular/platform-browser-dynamic": "^6.0.3",
"@angular/router": "^6.0.3",
"core-js": "^2.5.4",
"firebase": "^7.7.0",
"moment": "^2.24.0",
"ng-pick-datetime": "^6.0.16",
"ng-pick-datetime-moment": "1.0.7",
"ngx-image-cropper": "^2.0.2",
"ngx-smart-modal": "^7.1.1",
"ngx-textarea-autosize": "^2.0.3",
"ngx-toastr": "^10.0.4",
"rxjs": "6.3.3",
"zone.js": "^0.8.26"
},
...
}
main.ts
import { enableProdMode } from '@angular/core';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './app/app.module';
import { environment } from './environments/environment';
import { FirebaseOptionsToken, AngularFireModule } from '@angular/fire';
if (environment.production) {
enableProdMode();
}
platformBrowserDynamic().bootstrapModule(AppModule)
.catch(err => console.log(err));