最简示例:

export class AppComponent {
  title = 'angular-tour-of-heroes';
  // Create an Observable that will start listening to geolocation updates
  // when a consumer subscribes.
  locations = new Observable((observer) => {
    setTimeout(
      ()=>observer.next('hello'),
      3000
    )

    // When the consumer unsubscribes, clean up data ready for next subscription.
    return {
      unsubscribe() {
        console.log('unSubscribe...')
      }
    };
  });


  handleClick() {
    // Call subscribe() to start listening for updates.
    const locationsSubscription = this.locations.subscribe({
      next(position) {
        console.log('Current Position: ', position);
      },
      error(msg) {
        console.log('Error Getting Location: ', msg);
      }
    });

    // Stop listening for location after 10 seconds
    setTimeout(() => {
      locationsSubscription.unsubscribe();
    }, 5000);
  }

}

 

相关文章:

  • 2022-12-23
  • 2021-06-04
  • 2022-12-23
  • 2021-10-12
  • 2022-03-09
猜你喜欢
  • 2021-11-03
  • 2021-09-05
  • 2021-12-13
  • 2022-12-23
  • 2021-11-01
  • 2022-12-23
  • 2021-10-19
相关资源
相似解决方案