user9754798
user9754798

Reputation: 443

How do I programmatically find the path for an asset file I have in Angular?

I have a json file mappings.json in my assets/data directory. I've created a service with the intention of making a HTTP call from the service so that the file can be used within the app.

However the HTTP call I'm making always seems to return a 404.

constructor(private http: HttpClient) {
    this.http.get<PropMappings>("src/assets/data/mappings.json").subscribe(res => {
      this._propMappings = res;
      console.log(this._propMappings);
      });
  }

I've tried the following URLs:

src/assets/data/mappings.json
assets/data/mappings.json
projectName/src/assets/data/mappings.json

What do I need to do here? This link just says I need the second URL, but that doesn't work: https://www.angularjswiki.com/angular/how-to-read-local-json-files-in-angular/

Upvotes: 0

Views: 646

Answers (1)

Arash Hatami
Arash Hatami

Reputation: 522

start with dot like this:

./assets/data/mappings.json

Upvotes: 2

Related Questions