如何在异步管道中更新现有的Observable。

huangapple go评论57阅读模式
英文:

How to update existing Observable in async pipe

问题

I understand that you'd like the code portions to be translated. Here's the translation:

我有一个Observable通过async管道进行订阅

<ng-container *ngIf="invitations$ | async as invitations">

这个Observable在ngOnInit中设置

this.invitations$ = this.getInvitations();

getInvitations是一个执行http调用并返回Observable<Invitation[]>的私有函数

private getInvitations(): Observable<Invitation[]> {
    return this.invitationService.getInvitations();
}

现在在不同的函数中我想在http调用完成后更新这个Observable我想要发送一个提醒通过模板中的按钮触发),如果成功我想要更新邀请

sendReminder() {
    this.invitationService.postReminderEmail()
      .subscribe();
  }

所以我有这一行

this.invitations$ = this.getInvitations();

但是我应该把它放在哪里我知道async管道会处理取消订阅前一个值并在我这样做时重新订阅所以不会有内存泄漏

我应该把它放在管道中吗

sendReminder() {
    this.invitationService.postReminderEmail().pipe(
     mergeMap(() => this.invitations$ = this.getInvitations())
      .subscribe();
  }

我认为这一行是一个同步操作而且我在mergeMap中并没有真正返回任何东西尽管编译器接受这个),所以这似乎不对

我可以用tap替换mergeMap但我对此也不太确定

把它放在subscribe中感觉像是一个间接嵌套的subscribe因为它会通过async管道触发一个新的subscribe),这不是我想要的

我是否可以创建一个Subjectinvitations$可以监听它然后在这里使用next()来触发它我不喜欢的是我必须在next中放一个空数组或其他东西来触发它

this.invitationsSubject.pipe(
      mergeMap(() => this.getInvitations()),
      takeUntil(this.destroyed)
    ).subscribe();

this.invitations$ = this.invitationsSubject.asObservable();

触发

this.invitationsSubject.next([])

这也似乎不是一个好的解决方案而且更冗长)。

什么是正确的方法

Please note that translating code can sometimes result in a loss of clarity or context, so it's important to review the translated code carefully to ensure it meets your requirements.

英文:

I have an Observable that is subscribed to via the async-pipe.

&lt;ng-container *ngIf=&quot;invitations$ | async as invitations&quot;&gt;

This Observable is set in the ngOnInit:

this.invitations$ = this.getInvitations();

getInvitations is a private function that does the http-call and returns an Observable&lt;Invitation[]&gt;.

 private getInvitations(): Observable&lt;Invitation[]&gt; {
return this.invitationService.getInvitations();
}

Now, in a different function, I want to update this Observable, after a http-call has been completed. I want to send a reminder (triggered via a button in the template), and if it succeeds, I want to update the invitations.

sendReminder() {
this.invitationService.postReminderEmail()
.subscribe();
}

So I have the line:

this.invitations$ = this.getInvitations();

But where do I put it? I know that the async-pipe will handle the unsubscription from the previous value and will resubscribe when I do this, so no memory leak.

Do I put it in the pipe?

   sendReminder() {
this.invitationService.postReminderEmail().pipe(
mergeMap(() =&gt; this.invitations$ = this.getInvitations())
.subscribe();
}

I think this line is a synchronous action, and I'm not really returning anything in the mergeMap (even though the compiler accepts this), so this does not seem to be right.
I can change the mergeMap with tap, but I'm not sure about that either.
Putting it in the subscribe feels like an indirect nested subscribe (because it will trigger a new subscribe via the async pipe), which I don't want.
Do I make a Subject that the invitations$ can listen to, and I do a next() here, to trigger it? What I don't like about that, is that I have to put an empty array or something in the next to trigger it.

this.invitationsSubject.pipe(
mergeMap(() =&gt; this.getInvitations()),
takeUntil(this.destroyed)
).subscribe();
this.invitations$ = this.invitationsSubject.asObservable();

Trigger:

this.invitationsSubject.next([])

This also does not seem to be a good solution (and it is more verbose).

What is the right approach?

答案1

得分: 1

如果我理解您的意思正确,您只是想在发送提醒时更新 invitations$ 吗?

您可以直接更新 invitations$ 而不是创建一个新的可观察对象,Angular 会自动取消订阅先前的可观察对象。

sendReminder(){
  this.invitations$ = this.invitationService.postReminderEmail().pipe(switchMap(() => this.getInvitations()));
}
英文:

If I understand you correctly, you simply want to update the invitations$ when you send a reminder?

You could just update the invitations$ instead of creating a new observable, angular will unsubscribe from the previous observable.

sendReminder(){
this.invitations$ = this.invitationService.postReminderEmail().pipe(switchMap(() =&gt; this.getInvitations()));
}

huangapple
  • 本文由 发表于 2023年4月13日 18:00:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76004130.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定