欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

rxjs do / tap Property 'do' does not exist on type 'Observable<any>' 有大用 有大大用

return this.tap((next)=>{
        if(!environment.production){
            console.log(message,next);
        })


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

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



do / tap

函数签名: do(nextOrObserver: function, error: function, complete: function): Observable

Transparently perform actions or side-effects, such as logging.

透明地执行操作或副作用,比如打印日志。


:bulb: If you are using as a pipeable operator, do is known as tap!


示例

示例 1: 使用 do 输出日志

StackBlitz | jsBin | jsFiddle )

// RxJS v6+
import { of } from 'rxjs';
import { tap, map } from 'rxjs/operators';

const source = of(1, 2, 3, 4, 5);
// 使用 tap 透明地打印 source 中的值
const example = source.pipe(
  tap(val => console.log(`BEFORE MAP: ${val}`)),
  map(val => val + 10),
  tap(val => console.log(`AFTER MAP: ${val}`))
);

// 'tap' 并不转换值
// 输出: 11...12...13...14...15
const subscribe = example.subscribe(val => console.log(val));

其他资源


来自  https://rxjs-cn.github.io/learn-rxjs-operators/operators/utility/do.html

普通分类: