重构 observable 以在服务中调用 NgbModal

Refactor observable to call a NgbModal in a service

我在 angular 11 中有一项服务,用于在我关闭该组件时调用 Ngbmodal 组件进行订阅。这是代码:

  showMessage(messageData: MessageDataDTO): Observable<MessageResult> {
    return new Observable((result) => {
      const dialogRef = this.dialog.open(
        MessageComponent,
        this.ngbModalOptions
      );
      dialogRef.componentInstance.data = messageData;
      dialogRef.closed.subscribe((res) => {
        result.next(res);
        result.complete();
      });
    });
  }

我如何重构代码以省略 result.next(res); result.complete();

@Clashsoft 认为你是对的。我将代码重构为:

showMessage(messageData: MessageDataDTO): Observable<MessageResult> {
  const dialogRef = this.dialog.open(MessageComponent, this.ngbModalOptions);
  dialogRef.componentInstance.data = messageData;
  return dialogRef.closed;
}

已解决。