如何在可观察管道序列中使用材质对话框

And*_*ray 1 modal-dialog reactjs angular-material angular

对于我从 AngularJS 重写的应用程序,我有一系列操作:

1) I got to the server to get a message  
2) I display the returned message in a generic dialog, with a Yes and No button.  

3-Yes) I go to the server to do something, proceed to 4  
3-No) I terminate the sequence  

4) Notfiy the user that the operation is complete.
Run Code Online (Sandbox Code Playgroud)

然而,我在将其转换为 Angular/ReactObservable系统时遇到问题。我想做这样的事情:

// Actual arguments are immaterial in this case...
this.webDataService.get('/api/GetEndUserMessage', args)
  .pipe(
    map((message: string) => {
      const config = new MatDialogConfig();
      config.data = {
        'title': 'Important',
        'message': message
      };
      const dialog = this.matDialog.open(GenericDialogComponent, config);
      // If Yes/Ok is clicked, return 'Ok'
      // If No/Cancel is clicked, return 'Cancel'

      return dialog.afterClosed();
    }),
    // PROBLEM AREA! ----------------------------------------------
    filter((dialogResult: string) => {
      if (dialogResult === 'Ok')
        return this.webDataService.post('/api/DoSomethingAwesome');
    }),
    filter((dialogResult: string) => {
      if (dialogResult !== 'Ok')
        return 'Cancelled'
    })
    // PROBLEM AREA! ----------------------------------------------
  )
  .subscribe((result: any) => {
    if (result === 'Cancelled')
      return;

    // Let the user know that the sequence is over.  How doesn't matter.
  });
Run Code Online (Sandbox Code Playgroud)

问题是,显然无法编译。

我对 React 操作符系统的理解充其量是不稳定的,并且我不确定如何调用由于调用而生成的 Observable MatDialogRef.afterClosed()

问题:我可以通过什么方式使用序列中调用
的结果?MatDialogRef.afterClosed()Observable .pipe

Rea*_*lar 5

switchMap当你需要改变 Observable 流时使用 a 。运算map()符只会导致可观察对象发出返回值。

this.webDataService.get('/api/GetEndUserMessage', args)
  .pipe(
    switchMap((message: string) => {
        //...
        const dialog = this.matDialog.open(GenericDialogComponent, config);
        return dialog.afterClosed();
    }),
    switchMap((dialogResult: string) => {
        return (dialogResult === 'Ok')
            ? this.webDataService.post('/api/DoSomethingAwesome')
            : of('Cancelled')
    })
).subscribe((result: any) => { ... });
Run Code Online (Sandbox Code Playgroud)

https://www.learnrxjs.io/operators/transformation/switchmap.html