Subscribe (Subscription)
What is a subscription? Subscribe is an object that represents a complete handle on the release of (disposable) resources, is the implementation of a program of Observable. Subscribe to have a very important way, unsubscribe
it is no argument, just subscribe to the processing of resources. In previous versions of RxJS, Subscription called "Disposable".
import { interval } from 'rxjs';
const observable = interval(1000);
const subscription = observable.subscribe(x => console.log(x));
// 然后:
// 这个取消通过观察者订阅正在开始执行的 Observable
subscription.unsubcribe();
A subscription is merely a
unsubscribe()
function to release resource or cancel the execution of Observable.
Subscribe or put together, so that you can call a unsubscribe()
function to release more subscriptions. You can also "adding" to which is added a subscription to another subscription:
import { interval } from 'rxjs';
const observable1 = interval(400);
const observable2 = interval(300);
cosnt subscription = observable1.subscribe(x => console.log('first: ' + x));
const childSubscription = observable2.subscribe(x => console.log('second: ' + x));
subscription.add(childSubscription);
setTimeout(() => {
//取消订阅 subscription 以及 childSubscription
subscription.unsubscribe();
}, 1000);
When run, we will see the following results:
second: 0
first: 0
second: 1
first: 1
second: 2
Subscription (Subscription) is also a remove(otherSubscription)
method, in order to remove it from the child's subscription.