我不知道为什么,但是当我将 Observable 与扩展方法和管道一起使用时,管道图或水龙头没有触发。
import { Observable, Subscriber, Observer } from 'rxjs';
import { tap, map } from 'rxjs/operators';
...
declare module 'rxjs' {
interface Observable<T> {
useCache<T>(this: Observable<T>, key: string): Observable<T>;
}
}
Observable.prototype.useCache = function<T>(this: Observable<T>, key: string) {
// executed
const inCache = LocalStorage.get(key);
if (inCache) {
return new Observable<T>(observer => observer.next(inCache));
}
// this.pipe(tap((e: T) => {
// LocalStorage.set(key, e);
// }));
this.pipe(map((e: T) => {
//not executed
LocalStorage.set(key, e);
}));
return this;
};
...
(in somewhere component)
this.service.get(...).subscribe(e=>{
//executed!
});
在其他地方,我可以设置停在那里但不在映射 lambda 函数内部的断点
最佳答案
this
对象未被修改。尝试返回带有附加管道的this
。由于您不进行 map 绘制,因此只需使用点击
即可。
Observable.prototype.useCache = function<T>(this: Observable<T>, key: string) {
const inCache = LocalStorage.get(key);
if (inCache) {
return of(inCache);
}
return this.pipe(
tap((e: T) => LocalStorage.set(key, e))
);
};
关于angular - 订阅时不触发 rxjs pipeline tap/map,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60625918/