2017-09-18 54 views
0


你好,這裏是不承認一個JSON文件的項目一個問題 - 我不知道爲什麼。有什麼我需要改變或使其工作?GET HTTP://本地主機:4200/API/x.json 404(未找到) - 角2


這是我的文件夾: enter image description here


這是我的服務:

import { Injectable } from "@angular/core"; 
import { Ibrides } from "./brides"; 
import { HttpClient, HttpErrorResponse } from '@angular/common/http'; 
import { Observable } from 'rxjs/Observable'; 
import 'rxjs/add/observable/throw'; 
import 'rxjs/add/operator/catch'; 
import 'rxjs/add/operator/do'; 

@Injectable() 
export class brideService { 
private _brideUrl = 'api/brides.json'; 
constructor(private _http: HttpClient) { }; 

getBrides(): Observable<Ibrides[]> { 

    return this._http.get<Ibrides[]>(this._brideUrl) 
     .do(data => console.log('All:' + JSON.stringify(data))) 
     .catch(this.handleError) 
} 
private handleError(err: HttpErrorResponse) { 
    console.log(err.message); 
    return Observable.throw(err.message); 
} 
} 

這是我的組件


import { Component, OnInit } from '@angular/core'; 
import { Ibrides } from "./brides"; 
import { brideService } from "./brides.service" 

@Component({ 
selector: 'pm-brides', 
templateUrl: './brides_list.component.html', 
styleUrls: [] 
}) 
export class bridesListComponent implements OnInit { 


    constructor(private _brideService: brideService) { 

    } 
    errorMessage: string; 
    brides: Ibrides[] = []; 
    ngOnInit(): void { 
    this._brideService.getBrides() 
     .subscribe(brides => { 
      this.brides = brides 
     }, 

     error => this.errorMessage = <any>error); 

    } 

} 
+0

很明顯,因爲該URL沒有可用的API。正如我看到一個json文件,你不需要任何機會的模擬API調用? – mok

+0

我有一個內部的json文件,我只是想給他打電話。 –

+0

將'_brideUrl'設置爲'app/api/brides.json',即'_brideUrl ='app/api/brides.json'。 – mok

回答

0

剛剛從根級引用該文件是這樣的:

_brideUrl = 'app/api/brides.json' 

欲瞭解更多信息,可以參考this

相關問題