考虑以下情形(Angular v7):
第二点是这里的关键,看起来像这样:
@NgModule({
imports: [
...
OAuthModule.forRoot({
resourceServer: {
allowedUrls: [API_SERVER_URL], // <== we need to set the value that we loaded from the external endpoint (JSON) here
sendAccessToken: true
}
}),
...
],
declarations: [AppComponent],
bootstrap: [AppComponent]
})
export class AppModule { }
Run Code Online (Sandbox Code Playgroud)
我已经尝试了以下内容:
OAuthModule.forRoot()会在APP_INITIALIZER下载外部配置JSON 之前触发。main.ts进入角环境变量,然后引导中的AppModule。由于中的import { AppModule } from './app/app.module';语句也无法正常工作main.ts,这会导致AppModule在加载OAuthModule.forRoot()外部配置之前加载并启动(此注释确认了此行为)。main.ts …我需要将 Firebase 托管中的默认配置用于应用程序,因为我要将其部署到多个项目。
如果这是一个普通的 html 应用程序,您将使用:
<!-- The core Firebase JS SDK is always required and must be listed first -->
<script src="/__/firebase/6.1.0/firebase-app.js"></script>
<!-- TODO: Add SDKs for Firebase products that you want to use
https://firebase.google.com/docs/web/setup#reserved-urls -->
<!-- Initialize Firebase -->
<script src="/__/firebase/init.js"></script>
Run Code Online (Sandbox Code Playgroud)
但是我使用的是 angular 应用程序,所以我想在应用程序模块的初始化中注入它。我试图做这样的事情:
let firebaseConfig = environment.firebase;
if (environment.production) {
console.log('loading init');
fetch('/__/firebase/init.json').then(async response => {
firebaseConfig = await response.json();
});
}
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
AngularFireModule.initializeApp(firebaseConfig),
Run Code Online (Sandbox Code Playgroud)
这只是我的应用程序模块的一部分,但您可以大致了解一下。如果它在生产中,我想init.json从环境中提取它,但如果不是,我想从环境设置中提取它。
由于这是一个异步操作,我该如何使它工作?