Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.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
Rxjs 我希望有一个可以观察到的行为像一个承诺_Rxjs_Angular5_Rxjs5_Angular4 Httpclient - Fatal编程技术网

Rxjs 我希望有一个可以观察到的行为像一个承诺

Rxjs 我希望有一个可以观察到的行为像一个承诺,rxjs,angular5,rxjs5,angular4-httpclient,Rxjs,Angular5,Rxjs5,Angular4 Httpclient,var promise=http.get().toPromise()给出一个执行一次的承诺 因此,无论调用多少次promise.then(),http请求只执行一次 如何使用observable实现同样的效果?:var obbservable=http.get()。?这样,obversable.subscribe()只执行一次http请求?您可以像这样使用以下rxjs操作符“take” 导入操作符 import { take } from 'rxjs/operators'; 像这样使用它 th

var promise=http.get().toPromise()给出一个执行一次的承诺

因此,无论调用多少次
promise.then()
,http请求只执行一次


如何使用observable实现同样的效果?:
var obbservable=http.get()。?
这样,
obversable.subscribe()
只执行一次http请求?

您可以像这样使用以下rxjs操作符“take” 导入操作符

import { take } from 'rxjs/operators';
像这样使用它

this.http.get(`${this.url}/${agentId}/intents/${intentId}`)
            .pipe(take(1),
                catchError(error => Observable.throw(error)));

但这只会发出一个值,即最新的值,如果再次调用此函数,则不会阻止Observable调用请求。

您可以像这样使用以下rxjs运算符“take” 导入操作符

import { take } from 'rxjs/operators';
像这样使用它

this.http.get(`${this.url}/${agentId}/intents/${intentId}`)
            .pipe(take(1),
                catchError(error => Observable.throw(error)));
但这只会发出一个值,即最新的值,如果再次调用此函数,则不会阻止Observable调用请求。

以下代码执行了此操作: 从“rxjs/operators”导入{shareReplay}

这个.http.get(“xxx”).pipe(shareReplay(1))

以下代码完成了此操作: 从“rxjs/operators”导入{shareReplay}


这个.http.get(“xxx”).pipe(shareReplay(1))

使用
shareReplay
操作符。好了,这就成功了!!谢谢
shareReplay
接线员。好的,这就成功了!!谢谢