相关疑难解决方法(0)

Angular HTTP GET与TypeScript错误http.get(...).map不是[null]中的函数

我在Angular中遇到HTTP问题.

我只想要GET一个JSON列表并在视图中显示它.

服务类

import {Injectable} from "angular2/core";
import {Hall} from "./hall";
import {Http} from "angular2/http";
@Injectable()
export class HallService {
    public http:Http;
    public static PATH:string = 'app/backend/'    

    constructor(http:Http) {
        this.http=http;
    }

    getHalls() {
           return this.http.get(HallService.PATH + 'hall.json').map((res:Response) => res.json());
    }
}
Run Code Online (Sandbox Code Playgroud)

HallListComponentgetHalls从服务中调用方法:

export class HallListComponent implements OnInit {
    public halls:Hall[];
    public _selectedId:number;

    constructor(private _router:Router,
                private _routeParams:RouteParams,
                private _service:HallService) {
        this._selectedId = +_routeParams.get('id');
    }

    ngOnInit() {
        this._service.getHalls().subscribe((halls:Hall[])=>{ 
            this.halls=halls;
        });
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,我有一个例外:

TypeError:this.http.get(...).map不是[null]中的函数 …

rxjs angular

326
推荐指数
6
解决办法
23万
查看次数

标签 统计

angular ×1

rxjs ×1