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

时间:2017-09-18 06:02:12

标签: javascript json angular

您好,项目无法识别json文件存在问题 - 我不知道原因。有什么我需要改变或使其有效吗?

这是我的文件夹: https://mvnrepository.com/artifact/org.jooq/jooq-codegen-maven

这是我的服务:

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

    }

}

1 个答案:

答案 0 :(得分:0)

只需从根级引用该文件,如下所示:

_brideUrl = 'app/api/brides.json'

有关详细信息,请参阅this