Angular Google Maps(AGM):在app.module中获取Api键

时间:2018-12-13 08:18:38

标签: angular angular-google-maps

我在Angular 6应用程序中使用Angular Google Maps(ngm)。我想将api密钥存储在我的config.json文件中,并通过配置服务读取它。如下已在APP_INITIALIZER中调用了配置服务:

app.module.ts:

  export function initializeData(appConfig: AppConfig, authService: AuthService, localeService: LocaleService, clientConfigService: ClientConfigService, globalService: GlobalService) {
  return () => {
    try {
      return appConfig.load().then(() => {
        return Promise.all([authService.authenticateClient(), //...some other configs...//]).then((results) => {
          return Promise.resolve();
        }, (err) => {
          return Promise.reject(err);
        });
      });
    } catch (e) {
    }

  }
}

@NgModule({
providers: [
 AppConfig,
 {
      provide: APP_INITIALIZER,
      useFactory: initializeData,
      deps: [AppConfig, AuthService],
      multi: true
    },
],
imports: [
 AgmCoreModule.forRoot(),
]
});

我还导入了AgmCoreModule。我知道我必须编写一个提供程序来延迟加载api密钥,如下所示:

{ provide: LAZY_MAPS_API_CONFIG, useClass: GoogleMapConfigService },

在GoogleMapConfigService类中,我必须通过AppConfig.settings.googleMapsApiKey;

从配置中访问API KEY

问题是-appconfig.settings未定义,因为调用GoogleMapConfigService时可能未执行app_initializer。

该如何解决?

2 个答案:

答案 0 :(得分:0)

尝试像这样在“提供商”内声明AppConfig:

/deep/.sio-btn-group /deep/.sio-btn--primary-line

答案 1 :(得分:0)

我认为这是一个重复的问题。您可以尝试我对Angular 6.1+的回答:

https://stackoverflow.com/a/59407786/5795188