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

标签 javascript angular typescript rxjs

我在 Angular 中遇到 HTTP 问题。

我只想GET 一个JSON 列表并在 View 中显示它。

服务等级

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

HallListComponent 中,我从服务调用 getHalls 方法:

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

但是,我得到了一个异常(exception):

TypeError: this.http.get(...).map is not a function in [null]

hall-center.component

import {Component} from "angular2/core";
import {RouterOutlet} from "angular2/router";
import {HallService} from "./hall.service";
import {RouteConfig} from "angular2/router";
import {HallListComponent} from "./hall-list.component";
import {HallDetailComponent} from "./hall-detail.component";
@Component({
    template:`
        <h2>my app</h2>
        <router-outlet></router-outlet>
    `,
    directives: [RouterOutlet],
    providers: [HallService]
})

@RouteConfig([
    {path: '/',         name: 'HallCenter', component:HallListComponent, useAsDefault:true},
    {path: '/hall-list', name: 'HallList', component:HallListComponent}
])

export class HallCenterComponent{}

应用组件

import {Component} from 'angular2/core';
import {ROUTER_DIRECTIVES} from "angular2/router";
import {RouteConfig} from "angular2/router";
import {HallCenterComponent} from "./hall/hall-center.component";
@Component({
    selector: 'my-app',
    template: `
        <h1>Examenopdracht Factory</h1>
        <a [routerLink]="['HallCenter']">Hall overview</a>
        <router-outlet></router-outlet>
    `,
    directives: [ROUTER_DIRECTIVES]
})

@RouteConfig([
    {path: '/hall-center/...', name:'HallCenter',component:HallCenterComponent,useAsDefault:true}
])
export class AppComponent { }

tsconfig.json

{
  "compilerOptions": {
    "target": "ES5",
    "module": "system",
    "moduleResolution": "node",
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "removeComments": false,
    "noImplicitAny": false
  },
  "exclude": [
    "node_modules"
  ]
}

最佳答案

我认为你需要导入这个:

import 'rxjs/add/operator/map'

或者更一般地说,如果你想有更多的可观察方法。 警告:这将导入所有 50 多个运算符并将它们添加到您的应用程序中,从而影响您的包大小和加载时间。

import 'rxjs/Rx';

参见 this issue了解更多详情。

关于javascript - 带有 TypeScript 错误的 Angular HTTP GET http.get(...).map 不是 [null] 中的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34515173/

相关文章:

mysql - 如何向 Express 应用程序发出 PUT 请求

angular - : Can't resolve all parameters for 'Component' (? 错误,[object Object])

reactjs - react typescript : FunctionComponent with templated props

typescript - 最后一张幻灯片未调用 Ionic 5 Slides ionSlideDidChange

javascript - CanJS Observable 和键中的点

javascript - 如何从 grails RemoteLink 更新 TextField?

javascript - 如何为同一个类的多个元素设置不同的位置

javascript - Window.opener 在 IE8 中未定义?

javascript - 无法使用 Webpack、Typescript、自定义模块目录找到模块

javascript - 配置 Angular 2 HTTP 服务