In Angular2 you could have a folder /data/ and a json file there and you could access it at localhost:4200/data/something.json.
This is no longer possible in Angular4.
Any ideea how to get it to work?
Chrome allows you to access local JSON or other data files if you launch it with the --allow-file-access-from-files flag. I checked this with the code above on version 34.0. 1847.131 m; it should work on other versions as well.
Users can follow the below steps to open JSON files in Chrome or Firefox browsers. Right-click on the JSON file. Choose open with option from the menu. From the drop-down menu either choose Chrome or Firefox.
Steps to open JSON files on Web browser (Chrome, Mozilla)Open the Web store on your web browser using the apps option menu or directly using this link. Here, type JSON View in search bar under the Extensions category. You will get the various extensions similar to JSON View to open the JSON format files.
you can use this code
@Injectable()
export class AppServices{
constructor(private http: Http) {
var obj;
this.getJSON().subscribe(data => obj=data, error => console.log(error));
}
public getJSON(): Observable<any> {
return this.http.get("./file.json")
.map((res:any) => res.json())
.catch((error:any) => console.log(error));
}
}
here file.json
is your local json file.
see here also
also see the changlog of angular-cli for path
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With