2017-06-28 174 views
-4

我無法獲取JSON本地文件Angular HTTP服務。我有這些代碼:無法從JSON文件獲取數據

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

回答

2

由於您使用的角CLI該文件位置將不會在運行時提供給您(使用ng serve或生產版本)。將該文件放在資產文件夾中,然後您可以加載/assets/coins.json

+0

是的,它的工作!非常感謝 :) –