Angular7订阅者不是一个函数

Angular7 Subscriber is not a function

//myService.ts
//customhttpservice is just an observable returning http request response.



public getCPData(): Observable<any> {

    if (localStorage.getItem('resind')) {
     return of(JSON.parse(localStorage.getItem('resind')));
    } else {

      return this.customHttpService.get(url, headers, params, '')
      .toPromise()
      .then(resp => {
        localStorage.setItem('resind', resp)
        return resp;
       },
       error => console.error(error)
      );
    }
   }

//myComponent.ts


this.myService.getCPData().subscribe(data => {
      console.log('data', data);
    });

对于上面的代码,当我尝试订阅我的服务时,我收到订阅者不是功能错误。谁能帮我看看这里出了什么问题。

您不能订阅承诺,而是 return 一个可观察的:

 import { tap, catchError } from 'rxjs/operators';

 // ...

 return this.customHttpService.get(url, headers, params, '').pipe(
   tap(resp => localStorage.setItem('resind', resp))
   catchError( /** do stuff **/)
 )