28

升级到Angular 6.0和Rxjs到6.0之后,我收到以下编译错误:

Property 'do' does not exist on type 'Observable'.

这是代码:

import { Observable, of } from 'rxjs';
import 'rxjs/add/operator/do';
import 'rxjs/add/observable/throw';
import 'rxjs/add/operator/catch';
import { IProduct } from './product';

@Injectable()
export class ProductService { 
    constructor(
        private product: IProduct)
    {         
    }

    getProduct = () => { 
        return product.products
            // error on next line
            .do(data => console.log('All:' + JSON.stringify(data)))
            .catch(this.handleError);
    }

    private handleError(err: HttpErrorResponse) { 
        console.log(err.message);
        return Observable.throw(err.message);        
    }
}

任何想法?

3个答案 正确答案 

74

1) import {tap} from 'rxjs/internal/operators';

2) .do() 替换为  .pipe(tap())






问题不在于angular,而是rxjs。rxjs引入了对rxjs版本6的重大更改。

要使您的代码再次正常运行而不更改任何代码,请安装以下软件包:

npm install rxjs-compat@6 --save

然后,您应该能够编译您的项目。rxjs-compat只是暂时的解决方案,因此您需要更新代码库才能使用新版本。


新导入路径

您需要更新的内容:

  1. 更新来自的导入语句

    import { Observable } from "rxjs/Observable";

    import { Observable } from "rxjs";

  2. 更新您的运营商进口

    import 'rxjs/add/operator/do'

    import { do } from "rxjs/operators";


重命名运营商

由于与JavaScript保留字的名称冲突,一些运算符也已重命名。他们是

  1. do => tap

  2. catch => catchError

  3. switch => switchAll

  4. finally => finalize


无操作员链接

然后,您也无法再链接您的运算符,而需要使用该pipe运算符,例如

// an operator chain
source
  .map(x => x + x)
  .mergeMap(n => of(n + 1, n + 2)
    .filter(x => x % 1 == 0)
    .scan((acc, x) => acc + x, 0)
  )
  .catch(err => of('error found'))
  .subscribe(printResult);
// must be updated to a pipe flow
source.pipe(
  map(x => x + x),
  mergeMap(n => of(n + 1, n + 2).pipe(
    filter(x => x % 1 == 0),
    scan((acc, x) => acc + x, 0),
  )),
  catchError(err => of('error found')),
).subscribe(printResult);
12

Rxjs 6引入了一些重大更改,“ do”运算符已由“ tap”运算符代替(取自“ rxjs/internal/operators”)。

您可以使用新的运算符来重构代码,也可以通过添加rxjs-compat库以实现向后兼容性(npm install --save rxjs-compat)来仍然使用旧的'do'语法

请注意,在RxJs 6之前,您必须导入'do'运算符:

import 'rxjs/add/operator/do';

这里有更多详细信息:带有TypeScript错误http.get(...)。map的Angular HTTP GET不是[null]中的函数

  • 它已经被导入了,请参考问题所附的我的代码快照! –于尔根(  Urgen)'18年 5月8日在6:40
  • 正如上面提到,这是不是对rxjs 6.0或更高版本是正确的。你需要导入taprxjs/operators改变你的语法。或导入rxjs-compat@6(如果无法避免更新代码,则是“最后的选择”)。 –  paulsm4 '18 Dec 26在20:12

我感谢Tjaart van der Walt关于如何解决Angular / rxjs7 ++中引入的“重大更改”的响应。但是在尝试将他的响应应用于我的Angular拦截器时,我仍然遇到一些问题:

这是更新后的代码(编译失败的部分标记为“ OLD”)

import {Injectable} from '@angular/core';
import {HttpEvent, HttpInterceptor, HttpResponse} from '@angular/common/http';
import {HttpHandler, HttpRequest, HttpErrorResponse} from '@angular/common/http';

/*
  OLD:
  import {Observable} from 'rxjs/Observable';
  import 'rxjs/add/operator/do';
 */
import { Observable } from 'rxjs';
import { of } from 'rxjs';
import { tap, catchError } from 'rxjs/operators';

import { AuthService } from './auth.service';

@Injectable()
export class StockAppInterceptor implements HttpInterceptor {

  constructor(private authService: AuthService) {}

  intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    if (this.authService.authToken) {
      const authReq = req.clone({
        headers: req.headers.set(
          'Authorization',
          this.authService.authToken
        )
      });
      console.log('Making an authorized request');
      req = authReq;
    }
    /*
     * OLD:
     * return next.handle(req)
     *   .do(event => this.handleResponse(req, event),
     *      error => this.handleError(req, error));
     */
    return next.handle(req).pipe(
      tap(
        event => this.handleResponse(req, event),
        error => this.handleError(req, error)
      )
    );
  }


  handleResponse(req: HttpRequest<any>, event) {
    console.log('Handling response for ', req.url, event);
    if (event instanceof HttpResponse) {
      console.log('Request for ', req.url,
          ' Response Status ', event.status,
          ' With body ', event.body);
    }
  }

  handleError(req: HttpRequest<any>, event) {
    console.error('Request for ', req.url,
          ' Response Status ', event.status,
          ' With error ', event.error);
  }
}

所需的改变包括改变import的路径,而代pipe()tap()of()

此链接也是RxJS6更改的好资源:

https://www.academind.com/learn/javascript/rxjs-6-what-changed/

来自  https://stackoverflow.com/questions/50209119/property-do-does-not-exist-on-type-observableiproduct