Sooraj Bathery
Sooraj Bathery

Reputation: 155

Import Json file from asset - Angular Production Build Issue

I am importing my json file from asset folder to read some configurations inside the app which works perfectly and I can change the json values based on my requirement

you can refer : https://www.techiediaries.com/import-local-json-files-in-typescript/

But in the production build version, When try to change the values from json file in the asset folder, it reads development data

after the investigation we found that json data are embedded in main-es5 file,

is there is any way to read json file using import method and we can change data dynamically (based on environment)

Upvotes: 2

Views: 1940

Answers (2)

Avinash Dalvi
Avinash Dalvi

Reputation: 9311

This is another alternative solution you can try.

  1. Create one app.constant.ts file
    //
    // ===== File app.constants.ts    
    //
    'use strict';
    export const appConstants = {

       'key':'value',
       'key1':'value'
    };
  1. Import like this in component :
    import {appConstants} from './../app.constants';
  1. Use in component like this :

    this.variable = appConstants.key;

Upvotes: -1

Abhinav Kumar
Abhinav Kumar

Reputation: 3036

I would suggest the cleaner way to load it via HttpClient, and if it contains some configuration, I think this is the way you can change the data in assets and app will load again and again with HttpClient.

And if you want to read based on the environment, typescript import doesn't work.

Create a service, read the JSON via HttpClient based on pre-placed environment-specific JSON.

Upvotes: 2

Related Questions