2

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)

1
  • 2
    You'd have to fetch the json file with the HttpClient. I don't think it's possible with the typescript import feature as this will read a bundled version created at compile time as you already found out. If your production json file is already known at compile time you could of course bundle that file instead. Commented Dec 10, 2019 at 12:26

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

-1

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;

1 Comment

If we add in a ts file we can't change the value after the app build

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.