Angular 如何在rxjs中显式触发interval autoRefreshGateways(){ 返回间隔(网关刷新间隔) .烟斗( startWith(0), takeUntil(此为stopAutoRefresh), exhaustMap(()=>this.fetchAllData()) ).订阅( (网关)=>{ this.gateways=网关; }, () => { this.notificationsService.error(this.translateService.instant('zerotrust.gateway.list.error'),“”) } ); }

Angular 如何在rxjs中显式触发interval autoRefreshGateways(){ 返回间隔(网关刷新间隔) .烟斗( startWith(0), takeUntil(此为stopAutoRefresh), exhaustMap(()=>this.fetchAllData()) ).订阅( (网关)=>{ this.gateways=网关; }, () => { this.notificationsService.error(this.translateService.instant('zerotrust.gateway.list.error'),“”) } ); },angular,rxjs,Angular,Rxjs,我有一个autoRefresh函数,它每5秒调用一次api。在某些情况下,我不想等待5秒钟。我想根据UI中的某些操作立即调用fetchAllData。我为调用fetchAllData的函数编写了一个不同的函数。但是,有更好的解决方法吗?我建议使用主题,它将与时间间隔相结合。如果间隔或主体发出,则触发该事件 forceTrigger$=新主题(); 自动刷新网关(){ 返回组合测试( 间隔(网关刷新间隔), 此.forceTrigger$.pipe(startWith(true)) ) .烟斗(

我有一个autoRefresh函数,它每5秒调用一次api。在某些情况下,我不想等待5秒钟。我想根据UI中的某些操作立即调用fetchAllData。我为调用fetchAllData的函数编写了一个不同的函数。但是,有更好的解决方法吗?

我建议使用
主题
,它将与时间间隔相结合。如果间隔或主体发出,则触发该事件

forceTrigger$=新主题();
自动刷新网关(){
返回组合测试(
间隔(网关刷新间隔),
此.forceTrigger$.pipe(startWith(true))
)
.烟斗(
startWith(0),
takeUntil(此为stopAutoRefresh),
exhaustMap(()=>this.fetchAllData())
).订阅(
(网关)=>{
this.gateways=网关;
},
() => {
this.notificationsService.error(this.translateService.instant('zerotrust.gateway.list.error'),“”)
}
);
}
//强制触发的示例
行动(){
this.forceTrigger$.next(true);
}

您可以将延迟作为参数发送给函数,并使用RxJS作为可观察的源,而不是
interval()

试试下面的方法

控制器

autoRefreshGateways(延迟=5000,间隔=5000){//this.fetchAllData()
).订阅(
(网关)=>{
this.gateways=网关;
},
() => {
this.notificationsService.error(this.translateService.instant('zerotrust.gateway.list.error'),“”)
}
);
}
现在可以调用函数accd了。符合你的要求

从用户界面输入

。。。
自動更新
从控制器中的一些其他函数

someFunc(){
...

这个.autoRefreshGateways();//我想
merge
是你在这里的朋友

尝试通过思考触发更新的信号来解决问题:在您的情况下,您有两个来源:自动刷新和强制刷新。这很容易表示:

const forceRefresh = new Subject(); // In your case, you'd have this defined as an instance property of your class, so it can be called from outside.
const autoRefreshes = interval(GATEWAY_REFRESH_INTERVAL)
  .pipe(
    startWith(0),
    takeUntil(this.stopAutoRefresh)
  );
然后,您可以使用merge将这两个信号连接在一起,并调用所需的API:

merge(
  forceRefresh,
  autoRefreshes
).pipe(
  exhaustMap(() => this.fetchAllData())
).subscribe(/* ... */)