简体   繁体   English

this.http.get(...).map 不是函数

[英]this.http.get(…).map is not a function

I am using Angular 5.0.3.The error occurs when I created an promise and tried to subscribe it.我正在使用 Angular 5.0.3。当我创建一个承诺并尝试订阅它时发生错误。 When i try to execute my code, I get the following error as in the uploaded image.当我尝试执行我的代码时,我收到了上传图像中的以下错误。 上传 . . I tried with every possibility to solve this error, but unfortunately I can't able to solve it.我尝试了各种可能来解决这个错误,但不幸的是我无法解决它。

Elite-api.ts Elite-api.ts

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

//import { HttpClient } from "@angular/common/http";

//import 'rxjs';
//import 'rxjs/add/operator/map'
import { map } from 'rxjs/operators';
//import 'rxjs/add/operator/catch';

import { Observable } from 'rsjs/Observable';

@Injectable()
export class EliteApi{

    private baseUrl = 'https://elite-schedule-c1e63.firebaseio.com//';

    currentTourney:any = {};

    constructor(private http: Http){}

    getTournaments(){
        return new Promise(resolve => {
            this.http.get(`${this.baseUrl}/tournaments.json`) 
                .subscribe((res: Response) => resolve(res.json()));
        });

        }


    getTournamentData(tourneyId) : Observable<any>{
        return this.http.get('${this.baseUrl}/tournaments-data/${tourneyId}.json')
            .map((res: Response) => {
                this.currentTourney = res.json();
                return this.currentTourney;
            });
    }

}

teams.js团队.js

import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';

import { TeamHomePage } from '../team-home/team-home';

import { EliteApi } from '../../shared/elite-api'

@IonicPage()
@Component({
  selector: 'page-teams',
  templateUrl: 'teams.html',
})
export class TeamsPage {

  public teams = [];

  constructor(public navCtrl: NavController, 
              public navParams: NavParams,
              private eliteApi: EliteApi) 
              {}

  ionViewDidLoad(){

    let selectedTourney = this.navParams.data;

    this.eliteApi.getTournamentData(selectedTourney.id).subscribe(data => {
        this.teams = data.teams;
    });

  }

  itemTapped($event, team){
  this.navCtrl.push(TeamHomePage,team);
  }

}

tournaments.ts锦标赛.ts

import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';

import { TeamsPage } from '../teams/teams';

import { EliteApi } from '../../shared/elite-api'

@IonicPage()
@Component({
  selector: 'page-tournaments',
  templateUrl: 'tournaments.html',
})

export class TournamentsPage {

  public tournaments:any;


  constructor(
  public navCtrl: NavController, 
  public navParams: NavParams, 
  private eliteApi : EliteApi) {
  }

  ionViewDidLoad(){
    this.eliteApi.getTournaments().then(data => this.tournaments = data);
    console.log("Loaded Successfully");
  }


  itemTapped($event, tourney){
  this.navCtrl.push(TeamsPage, tourney);
  }

}

You are using the dot chain operator .map .您正在使用点链运算符.map The import should be进口应该是

import 'rxjs/add/operator/map';

import { map } from 'rxjs/operators';

refers to the pipeable operator map指的是可管道操作符map

this.http.get(...).pipe(
    map(...)
);

Pipeable Operators Doc可管道操作符文档

For Angular versions 5 and above, the updated importing line looks like :对于 Angular 5 及更高版本,更新后的导入行如下所示:

import { map } from 'rxjs/operators';

OR

import { map } from 'rxjs/operators';

Also these versions totally supports Pipable Operators so you can easily use .pipe() and .subscribe().此外,这些版本完全支持 Pipable Operators,因此您可以轻松使用 .pipe() 和 .subscribe()。 So this is how your code should look like after using .pipe().所以这就是你的代码在使用 .pipe() 之后的样子。 :

getTournamentData(tourneyId) : Observable<any>{
        return this.http.get('${this.baseUrl}/tournaments-data/${tourneyId}.json')
            .pipe(
            .map((res: Response) => {
                this.currentTourney = res.json();
                return this.currentTourney;
            })
          );
    }

If you are using Angular version 2, then the following line should work absolutely fine :如果您使用的是 Angular 版本 2,那么以下行应该可以正常工作:

import 'rxjs/add/operator/map';

OR

import 'rxjs/add/operators/map';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM