Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 订阅时不会触发rxjs管道攻丝/映射_Angular_Typescript_Rxjs_Extension Methods - Fatal编程技术网

Angular 订阅时不会触发rxjs管道攻丝/映射

Angular 订阅时不会触发rxjs管道攻丝/映射,angular,typescript,rxjs,extension-methods,Angular,Typescript,Rxjs,Extension Methods,我不知道为什么,但当我使用Observable with extension方法和管道时,管道贴图或水龙头不会触发 import { Observable, Subscriber, Observer } from 'rxjs'; import { tap, map } from 'rxjs/operators'; ... declare module 'rxjs' { interface Observable<T> { useCache<T>(th

我不知道为什么,但当我使用Observable with extension方法和管道时,管道贴图或水龙头不会触发

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!
});
import{observatable,Subscriber,observator}来自'rxjs';
从“rxjs/operators”导入{tap,map};
...
声明模块“rxjs”{
可观测界面{
useCache(this:Observable,key:string):Observable;
}
}
Observable.prototype.useCache=函数(this:Observable,key:string){
//执行
const inCache=LocalStorage.get(key);
if(inCache){
返回新的可观察对象(observer=>observer.next(inCache));
}
//这个管道(水龙头((e:T)=>{
//LocalStorage.set(key,e);
// }));
此.pipe(映射((e:T)=>{
//未执行
LocalStorage.set(key,e);
}));
归还这个;
};
...
(在组件中)
this.service.get(…).subscribe(e=>{
//执行!
});

在其他任何地方,我都可以设置断点,这些断点停止在那里,但不在map lambda函数内部

该对象未被修改。尝试在连接管道的情况下返回此。因为您没有映射,所以只需使用
点击

Observable.prototype.useCache=function(this:Observable,key:string){
const inCache=LocalStorage.get(key);
if(inCache){
返回(inCache);
}
把这个还给我(
轻触((e:T)=>LocalStorage.set(键,e))
);
};

of(inCache)是否可以返回
of(inCache)
而不是长的
新的可观察对象(observer=>observer.next(inCache))
?这是一个更好的选择。我盲目地复制和粘贴,然后调整了一些其他的位,但错过了一个。干杯