I am building a simple Angular app.
I have some static data in a JSON file which I want to load.
I have put the file data.json
under src
.
I am trying to load it as follows
export class AppComponent {
private urlDataFile = './data.json';
constructor(
private _http: HttpClient
) {
this.loadData().subscribe((data) => {
console.info(data);
});
}
private loadData() {
return this._http.get(this.urlDataFile);
}
}
and am running my server with ng serve
.
At runtime (on page load), I see the GET
request to http://localhost:4200/data.json
, and it results in a 404 NOT FOUND
I have tried putting this file elsewhere in the project - /src
, /src/app
, in the root of the project - with no success.
Where should this file be located? Or am I fundamentally doing it wrong?