tetra master
tetra master

Reputation: 567

Can't get data from JSON file

I can't get JSON local file Angular HTTP service. I have these codes:

import {Injectable} from '@angular/core';
import {Http, Response} from "@angular/http";
import 'rxjs/add/operator/map'


@Injectable()
export class ApiService {

constructor(private http:Http) {
}


private coinsUrl:string = 'app/data/coins.json';

getMines() {
  return this.http.get(this.coinsUrl).subscribe(
    (res:Response)=> {
      const sss = res.json();
      console.log('sss', sss);
    }

  );
}

}

it's my folder tree

Upvotes: -2

Views: 2642

Answers (1)

Brian Walker
Brian Walker

Reputation: 8928

Since you are using angular-cli that file location will not be available to you at runtime (either using ng serve or a production build). Put the file in the assets folder and then you can load /assets/coins.json.

Upvotes: 3

Related Questions