Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

264
Views
Angular4: ¿cómo acceder a json local?

En Angular2, podría tener una carpeta /data/ y un archivo json allí y podría acceder a él en localhost: 4200/data/something.json.

Esto ya no es posible en Angular4.

¿Alguna idea de cómo hacer que funcione?

about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

puedes usar este codigo

 @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

  • Cómo obtener un archivo json en angular2 usando la clase Http

también vea el registro de cambios de angular-cli para la ruta

  • https://github.com/angular/angular-cli/blob/master/CHANGELOG.md#100-rc4-2017-03-20
about 4 years ago · Santiago Trujillo Report

0

Por supuesto que es posible. Supongamos que aquí está su archivo json

ingrese la descripción de la imagen aquí


Y aquí está tu código para llamar al 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; }); } }
about 4 years ago · Santiago Trujillo Report

0

También puede usar "requerir";

 let test = require('./test.json');
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!