En Angular2 podrías tener una carpeta /data/ y un archivo json allí y podrías acceder a él en localhost:4200/data/something.json.
Esto ya no es posible en Angular4.
¿Alguna idea de cómo hacerlo funcionar?
puede utilizar este código
@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));
}
}
aquí file.json
es su archivo json local.
ver aquí también
también ver el changlog de angular-cli para la ruta
Por supuesto que es posible. Supongamos que este es su archivo json
Y aquí está tu código para llamar al archivo json
import { Injectable } from '@angular/core';
import { Http, Headers, Response } from '@angular/http';
import { Observable } from 'rxjs';
import 'rxjs/add/operator/map'
@Injectable()
export class YourService {
constructor(private http: Http) { }
getAdvantageData(){
let apiUrl = './assets/data/api/advantage.json';
return this.http.get(apiUrl)
.map( (response: Response) => {
const data = response.json();
return data;
});
}
}