【问题标题】:TypeError: this.http.post(...).map is not a function after update angular 5 to angular 6TypeError: this.http.post(...).map 在将 angular 5 更新为 angular 6 后不是函数
【发布时间】:2018-05-30 03:21:30
【问题描述】:

在将 Angular 5 更新到 Angular 6 后,我遇到了一些关于 rxjs6 的问题:

TypeError: this.http.post(...).map is not a function

error TS2339: Property 'map' does not exist on type 'Observable<Object>'.

TypeError: rxjs__WEBPACK_IMPORTED_MODULE_1__.Observable.of is not a function

我尝试了一些方法,例如:

将此导入添加到 service.ts

从 'rxjs/operators' 导入 { map };

更改 http.post().pipe(map(res => {...}))

但是,所有这些都不适合我。

我的环境如下:

 "@angular/cli": "^6.0.3"
 "rxjs": "^6.2.0"
 "rxjs-compat": "^6.2.0"

代码如下所示 服务.ts

import { Injectable } from '@angular/core';
import {environment} from '../../environments/environment';
import {HttpClient} from '@angular/common/http';
import {StorageService} from '../services/storage.service';

@Injectable()
export class VariationService {
ip = environment.url.management;
constructor(private http: HttpClient,
            private storageService: StorageService) { }
getFlowChart(status?) {
    status = status ? status : '';
    let token = this.storageService.getToken('token');
    return this.http.post(
        `${this.ip}/workflow`,
        {
            'access_token': token,
            'type': 'adjustment_workflow_get',
            'data': {
                'status': status
            }
        }
    ).map((res: Response) => {
      if ( res['errcode'] !== '00000') {
        return [];
      }
      return res['datas'];
    });
}
}

另一个问题的打字稿文件

import {Injectable} from '@angular/core';
import { PreloadingStrategy, Route } from '@angular/router';
import { Observable } from 'rxjs';

@Injectable()
export class SelectivePreloadingStrategy implements PreloadingStrategy {
  preloadedModules: string[] = [];
  preload(route: Route, load: () => Observable<any>): Observable<any> {
    if (route.data && route.data['preload']) {
      this.preloadedModules.push(route.path);
     return load();
    } else {
      return Observable.of(null);
    }
  }
}

【问题讨论】:

  • 如果您可以在 jsfiddle 中重现错误,那么您将更有可能得到答案。
  • 好的,我会再次使用jsfiddle。
  • “不适合我”意味着他们都给出相同的错误信息?
  • @Henry 没错,将 import { map } from 'rxjs/operators' 添加到 service.ts,终端不打印错误,但是,IDE 仍然显示错误 'map 'map' does not exist在类型 'Observable' 上。 '。更改 http.post().pipe(map(res => {...})) 这种方法确实不能解决所有问题。
  • Angular6 不需要地图操作符来处理 API 数据

标签: angular rxjs angular6 rxjs6


【解决方案1】:

RxJS v5.5.2+ 已移至 Pipeable 运算符,以改进树抖动并更轻松地创建自定义运算符。 现在需要使用pipe 方法组合operators
Refer This
新建导入

import { map} from 'rxjs/operators';

示例

myObservable
  .pipe(filter(data => data > 8),map(data => data * 2),)
  .subscribe(...);

创建Observables的方法

以前

 import 'rxjs/add/observable/of';
            // or 
            import { of } from 'rxjs/observable/of
        const source = Observable.of(1, 2, 3, 4, 5);

            const subscribe = source.subscribe(val => console.log(val));

在 RXJS:6 中,语法也发生了变化并导入了 Observable.of 而不是 of

import { Observable, of } from 'rxjs';

    const source = of(1, 2, 3, 4, 5);

    const subscribe = source.subscribe(val => console.log(val));

修改后的代码

  import { Injectable } from '@angular/core';
import {environment} from '../../environments/environment';
import {HttpClient} from '@angular/common/http';
import {StorageService} from '../services/storage.service';
import {map} from 'rxjs/operators';

@Injectable()
export class VariationService {
ip = environment.url.management;
constructor(private http: HttpClient,
            private storageService: StorageService) { }
getFlowChart(status?) {
    status = status ? status : '';
    let token = this.storageService.getToken('token');
    return this.http.post(
        `${this.ip}/workflow`,
        {
            'access_token': token,
            'type': 'adjustment_workflow_get',
            'data': {
                'status': status
            }
        }
    ).pipe(map((res: Response) => {
      if ( res['errcode'] !== '00000') {
        return [];
      }
      return res['datas'];
    }));
}
} 

修改后的代码

  import {Injectable} from '@angular/core';
import { PreloadingStrategy, Route } from '@angular/router';
import { Observable,of } from 'rxjs';

@Injectable()
export class SelectivePreloadingStrategy implements PreloadingStrategy {
  preloadedModules: string[] = [];
  preload(route: Route, load: () => Observable<any>): Observable<any> {
    if (route.data && route.data['preload']) {
      this.preloadedModules.push(route.path);
     return load();
    } else {
      return of(null);
    }
  }
} 

【讨论】:

  • 提前致谢!我会试试这个并告诉你结果。
  • 很高兴能帮上忙 :) 你能把它标记为正确答案吗
猜你喜欢
  • 2020-12-31
  • 2018-10-21
  • 2019-02-22
  • 1970-01-01
  • 2023-03-26
  • 2017-07-19
  • 2018-10-17
  • 2016-07-11
  • 1970-01-01
相关资源
最近更新 更多