I found that the simplest way to achieve this is by adding the file.jsonfile.json file under folder: assetsassets.
No need to edit: There isn't .angular-cli.jsonany need to edit file .angular-cli.json.
Service
@Injectable()
export class DataService {
getJsonData(): Promise<any[]>{
return this.http.get<any[]>('http://localhost:4200/assets/data.json').toPromise();
}
}
Component
private data: any[];
constructor(private dataService: DataService) {}
ngOnInit() {
data = [];
this.dataService.getJsonData()
.then( result => {
console.log('ALL Data: ', result);
data = result;
})
.catch( error => {
console.log('Error Getting Data: ', error);
});
}
###Extra: Ideally
Ideally, you only want to have this in a devdevelopment environment, so to be bulletproof., create a variable on your environment.tsenvironment.ts file:
export const environment = {
production: false,
baseAPIUrl: 'http://localhost:4200/assets/data.json'
};
Then replace the URL on the http.get for ${environment.baseAPIUrl}.
And the environment.prod.tsenvironment.prod.ts file can have the production API URL.
Hope this helps!