属性 'map' 在类型 'Observable<Blob>' 上不存在

Property 'map' does not exist on type 'Observable<Blob>'

我想用这个Angular6代码实现文件下载:

休息API:

@GetMapping("export")
    public ResponseEntity<InputStreamResource> export() throws IOException {
        ClassPathResource pdfFile = new ClassPathResource(EXTERNAL_FILE_PATH);

        HttpHeaders headers = new HttpHeaders();
        headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
        headers.add("Pragma", "no-cache");
        headers.add("Expires", "0");

        return ResponseEntity.ok().headers(headers).contentLength(pdfFile.contentLength())
                .contentType(MediaType.parseMediaType("application/pdf"))
                .body(new InputStreamResource(pdfFile.getInputStream()));
    }

服务:

import {Injectable} from '@angular/core';
import {HttpClient, HttpParams} from "@angular/common/http";
import {Observable} from "rxjs/index";
import {environment} from "../../../environments/environment";
import {HttpUtils} from "../common/http-utils";
import { map } from 'rxjs/operators';

@Injectable({
  providedIn: 'root'
})
export class DownloadService {

  constructor(private http: HttpClient) {
  }

  downloadPDF(): any {
    return this.http.get(environment.api.urls.downloads.getPdf, { responseType: 'blob' }).map(
    (res) => {
        return new Blob([res.blob()], { type: 'application/pdf' });
    });
  }  
}

组件:

import {Component, OnInit} from '@angular/core';
import {DownloadService} from "../service/download.service";
import {ActivatedRoute, Router} from "@angular/router";
import {flatMap} from "rxjs/internal/operators";
import {of} from "rxjs/index";
import { map } from 'rxjs/operators';

@Component({
  selector: 'app-download',
  templateUrl: './download.component.html',
  styleUrls: ['./download.component.scss']
})
export class DownloadComponent implements OnInit {

  constructor(private downloadService: DownloadService,
              private router: Router,
              private route: ActivatedRoute) {
  }

  ngOnInit() {   
  }

  export() {               
    this.downloadService.downloadPDF().subscribe(res => {
      const fileURL = URL.createObjectURL(res);
      window.open(fileURL, '_blank');
    });         
  } 
}

当我开始 angular 时出现错误:src/app/panel/service/download.service.ts(17,91) 中的错误:错误 TS2339:属性 'map' 'Observable'.

类型不存在

map 导入代码的正确方法是什么? 当我点击下载按钮时没有任何反应。

您可能使用的是 Rxjs 5.5 或更高版本。

在 Rxjs 5.5 之后,您不能再将 map 之类的运算符直接链接到可观察值上。您必须使用 .pipe,然后传递逗号分隔的运算符列表。

import { map } from 'rxjs/operators';
...
downloadPDF(): any {
  return this.http.get(environment.api.urls.downloads.getPdf, {
    responseType: 'blob',
    observe: 'response'
  })
  .pipe(
    map((res: any) => {
      return new Blob([res.blob()], { type: 'application/pdf' });
    })
  );
}

这里有一个 Sample StackBlitz 供您参考。

顺便说一句,Explorer 是一个很棒的工具,可以检查 Rxjs 语法到目前为止的变化。

您可以这样调用请求,因为您使用的是 angular 6

downloadPDF(): any {
    return this.http.get(environment.api.urls.downloads.getPdf, { responseType: 'blob' });
  }  

更新: 导入 ResponseContentType

import {Http, ResponseContentType} from '@angular/http';