Reputation: 365
I have a JSON file kept in the asset folder and I am importing it in my .ts component. And whenever I build the project with ng build —prod the JSON file will be bundled inside the main..ts file, and if I change any content in the JSON file it will be not reflected in the angular app as it is reading the data from main..ts file. But this doesn’t fit my requirement. What I need is, the static JSON file should be in the asset folder itself, and when we access the angular app it should read the JSON data from the JSON file kept in the asset folder
Upvotes: 1
Views: 1051
Reputation: 8773
I think you should instead follow this strategy. You can use httpclient and call the data from the JSON file. You can create a separate service to read JSON file, or you can get this method in any other service.
import { HttpClient } from "@angular/common/http";
Inject Http in constructor
constructor(private httpClient: HttpClient){}
public getJson() {
this.httpClient.get('assets/data.json').subscribe(data => {
// do anything thing with data
console.log(data);
})
}
I think this way when you change anything in your JSON file you don't need to create build again.
Upvotes: 2