无法阅读属性' Injectable'未定义的角度

时间:2017-07-24 20:43:29

标签: angular

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

// format of Product Interface so typing mistakes will be avoided
import { IProduct } from './product';

@Injectable()

export class ProductService {
    private _productUrl = 'api/products/products.json';
    constructor(private _http: Http){
    }

    getProducts(): Observable<IProduct[]> {
        return this._http.get( this._productUrl )
            .map( (response: Response) => <IProduct[]>response.json() )
            .do(data => console.log("All: " + JSON.stringify(data)))
            .catch(this.handleErrors);
    }

    private handleErrors(error: Response){
        console.log('Error: ' + error);
        return Observable.throw(error.json().error || 'Server Error');
    }

}

在文件顶部导入可注入但在Chrome开发工具中出现错误 查看下面的Chrome开发者工具图片

Injectable Undefined

1 个答案:

答案 0 :(得分:0)

看起来您缺少Observable运算符的import语句。尝试通过其他导入语句将其添加到顶部。

import 'rxjs/add/operator/do';
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/map';
import 'rxjs/add/observable/throw';