简体   繁体   中英

Property 'pipe' does not exist on type 'void' Angular 9

i create a function for stram audio in angular:

 private streamObservable(url) {
    new Observable(observer => {
      // Play audio
      this.audioObj.src = url;
      this.audioObj.load();
      this.audioObj.play();

      const handler = (event: Event) => {
        observer.next(event);
      };

      this.addEvents(this.audioObj, this.audioEvents, handler);
      return () => {
        // Stop Playing
        this.audioObj.pause();
        this.audioObj.currentTime = 0;
        // remove event listeners
        this.removeEvents(this.audioObj, this.audioEvents, handler);
      };
    });
  }

and when i need to using that function it show me error:

function use:

 playStream(url) {
    return this.streamObservable(url).pipe(takeUntil(this.stop$));
}

and show this error:

Property 'pipe' does not exist on type 'void'.

whats the problem???how can i solve this problem???

You need to return the observable from the called function.

Like - return new Observable (observer => {

 private streamObservable(url) {
    return new Observable(observer => {
      // Play audio
      this.audioObj.src = url;
      this.audioObj.load();
      this.audioObj.play();

      const handler = (event: Event) => {
        observer.next(event);
      };

      this.addEvents(this.audioObj, this.audioEvents, handler);
      return () => {
        // Stop Playing
        this.audioObj.pause();
        this.audioObj.currentTime = 0;
        // remove event listeners
        this.removeEvents(this.audioObj, this.audioEvents, handler);
      };
    });
  }

The solution for me was a pretty simple mistake, in my case I was returning an observable but I wasn't calling the method properly. I was missing the () in the method call.

Original Code

this.service.method.pipe().subscribe();

Fixed Code

this.service.method().pipe().subscribe();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM