獲取JSON我有這樣的代碼在我的服務從文件打字稿
import {Injectable} from '@angular/core';
import {Http, Headers, RequestOptions} from '@angular/http';
import 'rxjs/add/operator/map';
import {Client} from "../clients/client";
import {Observable} from "rxjs/Observable";
@Injectable()
export class ClientsService {
private clientUrl = './client.json';
private headers = new Headers({ 'Accept': 'application/json' });
private options = new RequestOptions({ headers: this.headers });
private client : Client;
constructor(private http:Http) {}
getClient() : Observable<any>{
return this.http.get(this.clientUrl, this.options)
.map(res => res);
}
}
,並在我的組件我稱之爲:
this.client = this.clientsService.getClient()
.subscribe(data => {
console.log(data);
});
但我發現了404錯誤
但我有這json
文件在我的服務相同的文件夾是。
有什麼不對?
如果我複製引用它的'src /應用程序/服務/客戶端',如果我輸入的結果是一樣的。如果我在最後添加.json結果是相同的 – gsiradze
@gsiradze是您的基礎文件夾src? 'app/Services/client.json'如何? – echonax
非常感謝。花了我5小時 – gsiradze