首页 > 解决方案 > 在应用程序中使用 config.json 文件的参数

问题描述

我们正在使用谷歌地图 api,我们有一个密钥。我们已将此密钥编码在我们的模块文件之一中。我们想在 config.json 文件中使用这个密钥,这样它应该是安全的,当我们将更改推送到 git 时,它不应该是可用的,因为我们不会在 git 上推送我们的 config.json 文件。我对此一无所知。

移动内容.module.ts

@NgModule({
  imports: [
    AgmCoreModule.forRoot({
      apiKey: 'GOOGLE-API KEY',
      libraries: ['places']
    }),

hip-config.json

googleMapsApiKey: "GOOGLE-API KEY" <--- want to use here ONLY

我只想在 hip-config.json 文件中使用此键,并希望将其从 mobile-content.module.ts 文件中删除。

有没有办法暗示这样的事情?

标签: angularangular-ui-routerangularjs-scope

解决方案


导入hip-config.json你的模块文件

import config from "../config/hip-config.json";

替换模块中的硬编码密钥

```

@NgModule({
  imports: [
    AgmCoreModule.forRoot({
      apiKey: 'config.GOOGLE-API KEY',
      libraries: ['places']
    }),

```

确保hip-config.json已将其添加到,.gitignore以免意外被推送。


推荐阅读