1

I created a project using angular cli [ version - 6.1.3 ] and was trying to import a JSON file in the component as -

import * as serverJson from '../../config/appdetails.json';

I wanted to include the following in the typings.d.ts -

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

However, there is no typings.d.ts present in the project structure.

In such circumstance, what is the best way to import a JSON file in angular 6 ?

1 Answer 1

6

Just create typings.d.ts in that folder where yours JSON data is stored with this content:

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

Then you can import your data in this manner, you need to name it, dont use '*':

import appDetails from '../../config/appdetails.json';
Sign up to request clarification or add additional context in comments.

1 Comment

@josip you forgot to include declare at the start of d.ts file. by any chance if you could create plunkr for this solution I guess it will help alot.

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.