`switchMap`后的订阅不起作用。

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

Subscription after switchMap does not work

问题

我已经创建了一个确认对话框,根据用户的选择返回true或false。

这是打开对话框并处理响应的函数:

public action(id: number): void {
    this.dialogService.openDialog(ConfirmationComponent).afterClosed()
        .pipe(
            switchMap(
                (result: boolean) => {
                    if (result) {
                        return this.service.doSomething(id);
                    } else {
                        return of('cancelled');
                    }
                }
            )
        ).subscribe({
            next: (response: string) => {
                this.snackBarService.openSnackBar(response, 'valid');
            },
            error: (error: HttpErrorResponse) => {
                this.httpService.handleError(error);
            },
            complete: () => {
                this.getData();
            }
        }
    );
}

当对话框返回false时,Snackbar会弹出消息“cancelled”(如预期),并执行getData()函数。然而,当对话框返回true时,会执行doSomething(id)函数(如预期),但Snackbar不会弹出,也不会执行getData()

有没有人知道为什么它会这样行为?我无法弄清楚。

英文:

I have created a Confirmation dialog that returns true or false, based on the user's choice.

This is the function that opens the dialog and handles the responses:

public action(id: number): void {
    this.dialogService.openDialog(ConfirmationComponent).afterClosed()
        .pipe(
            switchMap(
                (result: boolean) => {
                    if (result) {
                        return this.service.doSomething(id);
                    } else {
                        return of('cancelled');
                    }
                }
            )
        ).subscribe({
            next: (response: string) => {
                this.snackBarService.openSnackBar(response, 'valid');
            },
            error: (error: HttpErrorResponse) => {
                this.httpService.handleError(error);
            },
            complete: () => {
                this.getData();
            }
        }
    );
}

When the dialog returns false, the snack-bar opens with the message cancelled (as expected) and the getData() function gets executed. However, when the dialog returns true, the doSomething(id) function gets executed (as expected), but the snack-bar does not show up, and getData() is not executed.

Does anybody have any idea why it's behaving like that? I can't figure it out.

答案1

得分: 1

这实际取决于this.service.doSomething(id)返回的内容。我猜它是一个Observable,但根据你的描述,它从未发出过next,也没有发出complete。它可能返回一个空的Observable,比如EMPTY或者只是new Observable()

所以这里的解决方案是确保this.service.doSomething(id);发出并完成。

英文:

This really depends on what this.service.doSomething(id) returns. I suppose it's an Observable but based on your description it never emits any next and no complete. It might return an empty Observable such as EMPTY or just new Observable().

So the solution here is to make sure that this.service.doSomething(id); emits and completes.

huangapple
  • 本文由 发表于 2023年7月23日 16:07:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76747222.html
匿名

发表评论

匿名网友

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

确定