相关疑难解决方法(0)

AppModule加载之前的角加载外部配置

考虑以下情形(Angular v7):

  1. 在加载AppModule 之前,从外部端点(JSON)加载配置参数(API服务器URL和Auth服务器URL)
  2. 将配置传递给AppModule(OAuth2模块)
  3. 使用AOT编译应用

第二点是这里的关键,看起来像这样:

@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)

我已经尝试了以下内容:

  • 使用APP_INITIALIZER的解决方案。这是行不通的,因为OAuthModule.forRoot()会在APP_INITIALIZER下载外部配置JSON 之前触发。
  • 加载配置与异步功能main.ts进入角环境变量,然后引导中的AppModule。由于中的import { AppModule } from './app/app.module';语句也无法正常工作main.ts,这会导致AppModule在加载OAuthModule.forRoot()外部配置之前加载并启动(此注释确认了此行为)。
  • 加载的AppModule动态main.ts …

typescript angular

6
推荐指数
3
解决办法
1159
查看次数

将默认 Firebase 配置注入 Angular 应用程序

我需要将 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从环境中提取它,但如果不是,我想从环境设置中提取它。

由于这是一个异步操作,我该如何使它工作?

firebase angular

5
推荐指数
1
解决办法
577
查看次数

标签 统计

angular ×2

firebase ×1

typescript ×1