首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在angular 7中的concatMap/switchMap中使用检查条件

如何在angular 7中的concatMap/switchMap中使用检查条件
EN

Stack Overflow用户
提问于 2021-08-18 04:43:23
回答 1查看 87关注 0票数 1

我正在尝试下载一个pdf格式的API响应。API-1将返回文件名,并将文件名作为API - 2的输入,我将下载pdf。对于积极的案例,它工作得很好。如果API - 1没有返回fileName,我就不应该调用API-2,而是要告诉用户popupDialog中不存在任何文件。

代码语言:javascript
运行
复制
this.pdf.pdfName(pdfInfo).pipe(
      tap(res => fileName = res.fileName),
//Inside concatMap am not able to handle this condition (getVersionPdfFile-observable/printEmptyAlert - just a matdialog)
      concatMap(res => !!res.fileName ? this.pdf.getVersionPdfFile(res.fileName) : this.printEmptyAlert())
    ).subscribe(fileResponse => {
      var newBlob = new Blob([fileResponse], { type: "application/pdf" });
      const data = window.URL.createObjectURL(newBlob);
      var link = document.createElement('a');
      link.href = data;
      link.download = fileName;
      link.click();
      window.URL.revokeObjectURL(data);
    });
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-08-18 05:01:50

当没有文件名时,您可以抛出错误(使用throwError),并在错误块中处理该错误:

导入throwError

代码语言:javascript
运行
复制
import { throwError } from 'rxjs';


this.pdf.pdfName(pdfInfo).pipe(
   tap(res => fileName = res.fileName),
   concatMap(res => !!res.fileName ? this.pdf.getVersionPdfFile(res.fileName) : 
      throwError('No file name'))
   ).subscribe(fileResponse => {
        var newBlob = new Blob([fileResponse], { type: "application/pdf" });
        const data = window.URL.createObjectURL(newBlob);
        var link = document.createElement('a');
        link.href = data;
        link.download = fileName;
        link.click();
        window.URL.revokeObjectURL(data);
     }, (error) => {
        // Handle error here
        if(error === 'No file name'){
          this.printEmptyAlert();
        }
 });
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68826674

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档