Angular 从config.json文件提供google maps api密钥

Angular 从config.json文件提供google maps api密钥,angular,typescript,google-maps-api-2,Angular,Typescript,Google Maps Api 2,我有一个config.json文件,其中我处理了所有URL和键。有一个名为“googleMapsApiKey”的参数,其中有我的“映射键”。我想为我的模块文件提供此密钥,以便我可以使用谷歌地图,但我无法提供它。下面是我的代码 config.json { "googleMapsApiKey": "MY KEY", } MobileContentModule.ts import { ConfigService } from '../../../HiP-CmsAngularApp/

我有一个config.json文件,其中我处理了所有URL和键。有一个名为“googleMapsApiKey”的参数,其中有我的“映射键”。我想为我的模块文件提供此密钥,以便我可以使用谷歌地图,但我无法提供它。下面是我的代码

config.json

{
      "googleMapsApiKey": "MY KEY",
   }
MobileContentModule.ts

import { ConfigService } from '../../../HiP-CmsAngularApp/app/config.service';

    @NgModule({
      imports: [
        AgmCoreModule.forRoot({
          apiKey: 'WANT TO PROVIDE THAT KEY HERE', ---> from config.json
          libraries: ['places']
        }),
        CommonModule,
        FormsModule,
有一个名为ConfigService的服务,我在其中访问config.json文件。我已在MobileContentModule.ts文件中导入此服务


有人能帮我从config.json文件中获取我的google api密钥吗?这样做的目的是,我们不想在github上公开此密钥。

将其导入到您的文件中,如下所示:

import data from './config.json';
[...]
AgmCoreModule.forRoot({
  apiKey: data.googleMapsApiKey
  libraries: ['places']
}),[...]
但是
typescript2+
会抱怨
找不到模块'config.json'
。 然后必须使用通配符声明,如

declare module "json!*" {
    const value: any;
    export default value;
}
(参见6.1+中的:

app.module.ts

import*as fromConfig from'config.json';
@NGD模块({
进口:[
AgmCoreModule.forRoot({
apiKey:fromConfig.googleJSAPIKey,
图书馆:[“地点”],
}),]
})
tsconfig.json:

{
“编译器选项”:{
“resolveJsonModule”:true,
“esModuleInterop”:true
}
}

我无法执行此操作,因为当我推送此代码时,它无法生成,因为TFS中不存在此config.json文件。我已经尝试过这种方法,但我仍然会尝试…导入后是否还有其他方法可以获取该密钥?因此问题与“如何导入它”无关,而是与“为什么构建服务器上没有该文件”?我不确定它是否与TFS相关…在推送我的代码后,我得到以下错误。config.json文件不在TFS获取代码的git repo中,这就是构建失败的原因。。。未找到模块:错误:无法解析C:\agent\u tfship\u work\1\s\app\mobile content'@ylerjen中的'../../hip config.json',我忘了提到它是有效的…我已声明了通配符组件并阅读了此密钥…感谢您的宝贵建议@ylerjen I遵循了这种方法,它在ng serve中工作得很好,但在部署中,密钥没有加载,映射到dev env