我在Angular中遇到了HTTP问题.

我只想创建一个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());
    }
}

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

然而,我有一个例外:

打字错误:这个.http.得到(…).map不是[null]中的函数

霍尔中心.组成部分

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{}

app.component

import {Component} from 'angular2/core';
import {ROUTER_DIRECTIVES} from "angular2/router";
import {RouteConfig} from "angular2/router";
import {HallCenterComponent} from "./hall/霍尔中心.组成部分";
@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'

或者更一般地说,如果你想要有更多的方法来观察的话. WARNING: This will import all 50+ operators and add them to your application, thus affecting your bundle size and load times.

import 'rxjs/Rx';

更多细节见this issue.

Angular相关问答推荐

使用带有展开/折叠行的mat-table数据显示树状 struct

如何访问Angular 16中网址栏中的第一个网址收件箱?

如何取回.angular/ache文件夹?

无法执行客户端功能Angular 17与ssr'

如何在Angular中执行一个操作条件为多个可观测值?

在不使用散列(#)路由的情况下刷新Angular 8的SPA时,删除404错误

Angular:将服务注入非独立组件导致应用程序中断

如何使用ANGLING HTTP.POST并将类型更改为键入的回复?

更改物料设计中的复选框勾选 colored颜色

在剑道UI网格中包装Excel导出的列

更改动态表单控制Angular 的值

如何使用来自不同可观测对象的值更新可观测对象

等待可观察性完成后再调用下一个

升级到 Ng 16 - npm 错误!对等依赖冲突:@angular/compiler-cli@16.2.1

手动关闭 SSE 连接:Angular

带有数据的 Angular 模板

如何在 Angular4 中访问组件的 nativeElement?

如何在angular 5 中获取基本网址?

Angular 2 Material - 如何居中进度微调器

Angular 4 设置下拉菜单中的选定选项