使用服务获取angular4中的数据

时间:2017-05-27 03:49:31

标签: angular

我是角度4的新手,我尝试在angular4中获取数据,但是我得到了这个错误。 Cannot get customer.json 404 not found。我认为这个网址是正确的,因为客户服务和customer.json在同一个文件夹中。这是我试过的代码。

服务

import { Injectable } from '@angular/core';
import {Http , Response} from '@angular/http';

const URL_CUSTOMER = './customer.json'

@Injectable()
export class CustomerService {
    constructor(private _http : Http) {}

    getCustomer(){
        return this._http.get(URL_CUSTOMER)
                .map((response:Response) =>response.json());
}



}

组件

import { Component, OnInit } from '@angular/core';
import {Observable} from 'rxjs/Rx';
import {CustomerService} from '../customer/customer.service';
@Component({
    moduleId: module.id,
    selector: 'customers',
    templateUrl: 'Customers.html',
    providers:[CustomerService]
})
export class CustomersComponent implements OnInit {
    customers:Observable<any[]>;

    constructor(private _customer : CustomerService) {}

    ngOnInit() {
        this.customers = this._customer.getCustomer();
    }
}

主要

import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import 'rxjs/Rx';
import { AppModule } from './app/app.module';

platformBrowserDynamic().bootstrapModule(AppModule);

请告诉我哪里错了。提前谢谢

0 个答案:

没有答案