TypeError:this.http.post(...).map在更新角度5到角度6之后不是函数

Ste*_*ina 3 rxjs angular angular6 rxjs6

在将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
Run Code Online (Sandbox Code Playgroud)

我尝试过一些方法,比如:

将此导入添加到service.ts

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

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

然而,所有这些都不适合我.

我的环境如下:

 "@angular/cli": "^6.0.3"
 "rxjs": "^6.2.0"
 "rxjs-compat": "^6.2.0"
Run Code Online (Sandbox Code Playgroud)

代码显示如下 Service.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'];
    });
}
}
Run Code Online (Sandbox Code Playgroud)

另一个问题是typscript文件

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);
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

Vik*_*kas 9

RxJS v5.5.2+已经转移到Pipeable运营商,以改善树木震动,并使创建自定义运算符更容易.现在operators需要使用pipe方法组合
参考这个
新的导入

import { map} from 'rxjs/operators';
Run Code Online (Sandbox Code Playgroud)

myObservable
  .pipe(filter(data => data > 8),map(data => data * 2),)
  .subscribe(...);
Run Code Online (Sandbox Code Playgroud)

创建方法 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));
Run Code Online (Sandbox Code Playgroud)

在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));
Run Code Online (Sandbox Code Playgroud)

修改代码

  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'];
    }));
}
} 
Run Code Online (Sandbox Code Playgroud)

修改代码

  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);
    }
  }
} 
Run Code Online (Sandbox Code Playgroud)