import { Observable } from 'rxjs'; const myObservable = new Observable(observer => { observer.next('Hello!'); observer.complete(); }); myObservable.subscribe(data => console.log(data));
import { Observable } from 'rxjs'; const myObservable = new Observable(observer => { let count = 0; setInterval(() => { observer.next(count); count++; }, 1000); return () => clearInterval(count); }); const subscription = myObservable.subscribe(data => console.log(data)); setTimeout(() => subscription.unsubscribe(), 5000);In this example, we create an `Observable` that emits a new integer value every second. We also return a function that clears the interval when the observable is unsubscribed. We then subscribe to this observable and log each emitted value to the console. Finally, we simulate unsubscribing after 5 seconds by calling `unsubscribe` on the subscription. Overall, `rxjs.rx` is a powerful package library for handling observables in TypeScript, and `subscribe` is a crucial method for working with these observables in real-time.