Angular不会导入显示错误的Rxjs

时间:2018-06-11 08:27:19

标签: angular rxjs

我在使用我的角应用程序中从db导入数据时出现问题,确切地说我认为我的代码是正确的但是导入Rxjs时遇到问题。我会在这里发布我的代码:

import {Component, Directive} from '@angular/core';
import {FormGroup, FormControl} from '@angular/forms';
import {Http, Response, Headers} from '@angular/http';
import {Router, ActivatedRoute, Params} from '@angular/router';
import 'rxjs/Rx';

@Component({
    selector: 'app-room',
    templateUrl: './room.component.html',
    styleUrls: ['./room.component.css']
})
export class classRoomComponent {
    public sobe: any = [];
    http: Http;
    router: Router;
    route: ActivatedRoute;
    data: Object[];
    public id: Number;

    constructor(route: ActivatedRoute, http: Http, router: Router) {
        this.http = http;
        this.router = router;
        this.route = route;
    }

    ngOnInit() {

        this.route.params.subscribe((params: Params) => {
            let id = params['id'];
            let headers = new Headers();
            headers.append('Content-Type', 'application/x-www-form-urlencoded');
            headers.append("token", localStorage.getItem("token"));
            this.http.get('http://localhost/hotel/getroom.php?id=' + id, {headers: headers}).map(res => res.json()).share().subscribe(data => {
                    this.data = data.data;
                },
                err => {
                    this.router.navigate(['./']);
                }
            );
        });
    }
}

我哪里出错了,我该怎么做才能获取数据?或者修复导入

ps这是我为rxjs得到的错误: 物业'地图'在类型' Observable'。

上不存在

1 个答案:

答案 0 :(得分:1)

在rxjs6 +中,您需要使用pipeable operators,您可以从' rxjs /运算符'中导入它们,在您只需要mapshare运算符的情况下解决方案因为你看起来像:

import { map, share } from 'rxjs/operators';

之后在代码中使用

this.http.get('http://localhost/hotel/getroom.php?id='+id,{headers:headers})
    .pipe(
        map(res => res.json()),
        share()
    )
    .subscribe(() => {})