Use config.json file's parameters in app

Issue

We are using google map api and we have a key. We have codede this key in one of our module file. We want to use this key in config.json file so that it should be secure and when we push our changes to git, it should not be available as we dont push our config.json file on git. I don’t have any idea about this.

mobile-content.module.ts

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

hip-config.json

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

I want to use this key in hip-config.json file only and want to remove it from mobile-content.module.ts file.

Is there any way to impliment such thing?

Solution

Import hip-config.json into your module file

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

Replace the hard-coded key in the module

“`

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

“`

Make sure that hip-config.json has been added to .gitignore so it doesn’t accidentally get pushed.

Answered By – Whamo12

Answer Checked By – Robin (AngularFixing Admin)

Leave a Reply

Your email address will not be published.