Angular4: how do access local json?

前端 未结 7 2194
傲寒
傲寒 2020-12-06 05:00

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 Angular

相关标签:
7条回答
  • 2020-12-06 05:53

    Ofcourse its possible. Let's assume here is your json file


    And here is your code to call the 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;
          });
       }  
    }
    
    0 讨论(0)
提交回复
热议问题