BBaysinger
BBaysinger

Reputation: 6987

Where to put files that a Angular application needs to load with Http?

I'm using http request to load a json file, but I'm not actually sure where the config file needs to live.

    this.http.get('env.json')
    .map(res => res.json()).catch((error: any, caught: Observable<any>) => {
        console.log('no');
        return Observable.throw(error.json().error || 'Server error');
    }).subscribe(() => {
        console.log('yes');
    });

After running ng serve, there is no dist folder. I don't think I see a directory where items will get copied from into a dist folder.

So where do I put files that my application needs to load with Http?

Upvotes: 0

Views: 213

Answers (1)

Pengyy
Pengyy

Reputation: 38171

If the env.json need to be bundled into dist folder and deployed with your other compiled source code, you can simply add it to your project's assets folder(any folder in your project will be ok) and also add assets folder at .angular-cli.json's assets array as below:

"assets": [
  "assets",     // any folder in your project which contains the config file will be ok
  ...
],

And for retrieving data of env.json via http, just use it's path as

this.http.get('assets/env.json')

Upvotes: 1

Related Questions