本文介绍了提供config.json文件中的google maps api密钥的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有config.json文件,其中包含所有的网址和密钥.有一个名为"googleMapsApiKey"的参数,其中有我的地图键".我想将此密钥提供给我的模块文件,以便我可以使用Google地图,但无法提供它.下面是我的代码.

I have config.json file where i have procided all urls and keys. There is one parameter called "googleMapsApiKey" where i have my "map key". I want to provide this key to my module file so that i can use google maps but I am unable to provide it. Below is my code.

config.json

config.json

{
      "googleMapsApiKey": "MY KEY",
   }

MobileContentModule.ts

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文件中.

There is one service called ConfigService where I am accessing my config.json file. I have imported this service in MobileContentModule.ts file.

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

Can anyone help me to get my google api key from config.json file? Purpose of this is, that we dont want to expose this key on github.

推荐答案

将其导入文件中:

import data from './config.json';
[...]
AgmCoreModule.forRoot({
  apiKey: data.googleMapsApiKey
  libraries: ['places']
}),[...]

但是TypeScript 2+会抱怨Cannot find module 'config.json'.然后,您必须使用通配符声明,例如

But TypeScript 2+ will complain about Cannot find module 'config.json'.Then you'll have to use a wildcard declaration like

declare module "json!*" {
    const value: any;
    export default value;
}

(请参见 https://www.typescriptlang.org /docs/handbook/modules.html#wildcard-module-declarations )

这篇关于提供config.json文件中的google maps api密钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 20:26