摘要
本文主要讲述了:
- 什么是 Subscription
- 如何取消订阅
- 是否取消订阅
- 清理战场
正文
什么是 Subscription
Subscription,意为订阅。
Subscription
是 RxJS 中的类。
订阅一个Observable
实例将返回一个Subscription
实例。Subscription
表示一种订阅关系。
如何取消订阅
示例:手动取消订阅
输入:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta http-equiv="X-UA-Compatible" content="ie=edge" /> <title>Document</title> <script src="https://unpkg.com/core-js-bundle/index.js"></script> <script src="https://unpkg.com/regenerator-runtime/runtime.js"></script> <script src="https://unpkg.com/@babel/standalone@7/babel.min.js"></script> <script src="https://unpkg.com/@reactivex/rxjs@6/dist/global/rxjs.umd.js"></script> <script type="text/babel"> const observable = new rxjs.Observable(function (subscriber) { subscriber.next(1); subscriber.next(2); subscriber.next(3);
setTimeout(function () { subscriber.next(4); }, 1e3); });
const subscription = observable.subscribe({ next(x) { console.log(x); }, });
subscription.unsubscribe(); </script> </head> <body></body> </html>
|
输出:
其他情况
若“订阅协议”中发生了错误,或调用了error()
或complete()
,订阅会被自动取消。
是否取消订阅
Subscription
实例拥有名为closed
的实例属性。
示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta http-equiv="X-UA-Compatible" content="ie=edge" /> <title>Document</title> <script src="https://unpkg.com/core-js-bundle/index.js"></script> <script src="https://unpkg.com/regenerator-runtime/runtime.js"></script> <script src="https://unpkg.com/@babel/standalone@7/babel.min.js"></script> <script src="https://unpkg.com/@reactivex/rxjs@6/dist/global/rxjs.umd.js"></script> <script type="text/babel"> const observable = new rxjs.Observable(function (subscriber) { subscriber.next(1); subscriber.next(2); subscriber.next(3); });
const subscription = observable.subscribe({ next(x) { console.log(x); }, });
console.log(subscription.closed);
subscription.unsubscribe();
console.log(subscription.closed); </script> </head> <body></body> </html>
|
清理战场
在“订阅协议”中返回一个函数,用于在取消订阅后清理战场。
示例:
输入:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta http-equiv="X-UA-Compatible" content="ie=edge" /> <title>Document</title> <script src="https://unpkg.com/core-js-bundle/index.js"></script> <script src="https://unpkg.com/regenerator-runtime/runtime.js"></script> <script src="https://unpkg.com/@babel/standalone@7/babel.min.js"></script> <script src="https://unpkg.com/@reactivex/rxjs@6/dist/global/rxjs.umd.js"></script> <script type="text/babel"> const observable = new rxjs.Observable(function (subscriber) { subscriber.next(1); subscriber.next(2); subscriber.next(3);
return function () { console.log('clear'); }; });
const subscription = observable.subscribe({ next(x) { console.log(x); }, }); subscription.unsubscribe(); </script> </head> <body></body> </html>
|
输出:
参考资料
本文对你有帮助?请支持我