简体   繁体   English

通过从后端获取数据来设置从 app.module 导入

[英]Set imports from app.module by getting data from backend

In Angular is it possible to set imports from app.module by getting data from backend?在 Angular 中,是否可以通过从后端获取数据来设置从 app.module 导入?

RecaptchaFormsModule,
NgxStripeModule.forRoot(CONFIG.stripeKey),
AgmCoreModule.forRoot({
  apiKey: CONFIG.keyFromDServer,
  libraries: ["places"]
}),

So set the CONFIG.keyFromDServer with a key from the server.因此,使用来自服务器的密钥设置 CONFIG.keyFromDServer。 Can APP_INITIALIZER do this, if so how or is there anyway? APP_INITIALIZER 可以做到这一点,如果是这样,或者无论如何?

AOT compilation does not allow non-statically analyzable values to be used in Angular decorators. AOT 编译不允许在 Angular 装饰器中使用非静态可分析的值。

There are several options.有几种选择。 Here is one that waits for the configuration from the server before boostrapping the application.这是一个在 boostrapping 应用程序之前等待来自服务器的配置。

// main.ts
import { enableProdMode } from '@angular/core';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';

import { AppModule } from './app/app.module';
import { configurationToken } from './app/configuration';
import { environment } from './environments/environment';

if (environment.production) {
  enableProdMode();
}

// Platform creation and bootstrapping of the application is delayed
// until we have loaded the configuration file.
fetch('/assets/configuration.json')
  .then(configuration =>
    platformBrowserDynamic([
      { provide: configurationToken, useValue: configuration },
    ]).bootstrapModule(AppModule))
  .catch(error => console.error(error));
// configuration.ts
import { InjectionToken } from '@angular/core';

// We create an interface for the configuration JSON object
export interface Configuration {
  readonly apiUrl: string;
  readonly timezone: string;
  readonly websocketUrl: string;
}

// We use a dependency injection token to access the configuration
// in our application.
export const configurationToken = new InjectionToken('Configuration');
// time.service.ts
import { Inject, Injectable } from '@angular/core';

import { Configuration, configurationToken } from './configuration';

@Injectable()
export class TimeService {
  constructor(
    @Inject(configurationToken) private configuration: Configuration,
  ) {}

  // Configuration is available synchronously since it's just an object
  // that can be injected.
  get timezone(): string {
    return this.configuration.timezone;
  }
}

More options in " Handling Angular environments in continuous delivery " by Kevin Kreuzer. Kevin Kreuzer 的“ 在持续交付中处理 Angular 环境”中的更多选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从 angular 中的 app.module 调用服务内的方法并使用 Imports 中的值 - How to call a method inside a service from app.module in angular and use the values in Imports App.Module 导入在 Ionic 页面上不起作用 - App.Module imports not working on Ionic pages Angular将HttpClient从app.module传递到另一个模块到服务 - Angular Pass HttpClient from app.module to another module to a service 如何从app.module中排除用于生产构建的模块? - How to exclude a module from app.module for production builds? 我的模块没有看到来自app.module的组件 - My module doesn't see components from app.module 从 app.module 中的本地 json 文件读取 api url - Read api url from local json file in app.module 从服务文件获取值到app.module的最佳方法 - Best way to get a value from service file to app.module 每当使用RouterModule.forChild()从app.module调用不同模块的Component时,整个页面都会重新加载 - Entire Page getting reloaded whenever calling a Component of different module from app.module using RouterModule.forChild() app.module 中声明的拦截器不会拦截来自一个延迟加载模块的调用 - Interceptor declared in app.module is not intercepting call from one lazy loaded module Angular Google Maps (AGM):在 app.module 中获取 Api Key - Angular Google Maps (AGM): getting the Api Key in the app.module
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM