簡體   English   中英

GET http:// localhost:4200 / api / x.json 404(未找到)-角度2

[英]GET http://localhost:4200/api/x.json 404 (Not Found) - angular 2



您好,項目無法識別json文件-我不知道為什么。 我有什么需要更改或使其起作用的嗎?


這是我的文件夾: 在此處輸入圖片說明


這是我的服務:

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);

    }

}

只需從根級別引用文件,如下所示:

_brideUrl = 'app/api/brides.json'

有關更多信息,您可以參考

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM