{"id":167074,"date":"2020-10-07T12:20:46","date_gmt":"2020-10-07T04:20:46","guid":{"rendered":"http:\/\/4563.org\/?p=167074"},"modified":"2020-10-07T12:20:46","modified_gmt":"2020-10-07T04:20:46","slug":"%e9%97%ae%e4%b8%80%e4%b8%aa%e5%85%b3%e4%ba%8e-rxjs-%e7%9a%84%e6%8a%80%e6%9c%af%e9%97%ae%e9%a2%98","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=167074","title":{"rendered":"\u95ee\u4e00\u4e2a\u5173\u4e8e RxJS \u7684\u6280\u672f\u95ee\u9898"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u95ee\u4e00\u4e2a\u5173\u4e8e RxJS \u7684\u6280\u672f\u95ee\u9898               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : vsomeone <\/span>  <span><i><\/i> 3<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u73b0\u5728\u6709\u4e00\u4e2a\u9700\u6c42\uff0c\u5c01\u88c5\u7684 class A \u5185\u5305\u542b\u4e86\u4e00\u4e2a private member B\uff0cB \u6709\u4e00\u4e2a\u5f00\u653e\u7684 observable C\uff0cA \u80fd\u591f\u8bbf\u95ee\u5230 C \u3002<\/p>\n<p>\u73b0\u5728\u9700\u8981\u901a\u8fc7 A \u5c06 C \u5c01\u88c5\u8d77\u6765\u7ed9\u5916\u754c\u7684\u4f7f\u7528\u8005\u5f62\u6210\u4e00\u4e2a\u65b0\u7684 API \uff08\u547d\u540d\u4e3a source$\uff09\u3002<\/p>\n<p>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u7531\u4e8e\u4e00\u4e2a A \u7684\u751f\u547d\u5468\u671f\u4e2d B \u53ef\u80fd\u4f1a\u53d8\u5316\uff0c\u56e0\u6b64\u4e0d\u80fd\u76f4\u63a5\u628a C \u66b4\u9732\u7ed9\u7528\u6237\uff08\u5373 source$ = C\uff0c\u5426\u5219\u5f53\u7528\u6237 source$.subscribe(cb) \u540e\uff0c\u5982\u679c B \u53d8\u5316\u5bfc\u81f4 C \u53d8\u5316\uff0c\u8fd9\u4e2a subscription \u5b9e\u9645\u4e0a\u4f1a\u5931\u6548\uff09\u3002<\/p>\n<p>\u76ee\u524d\u6211\u7684\u65b9\u6848\u662f\uff1a<\/p>\n<pre><code>class A {       private B: TypeOfB;     private subscriptionToC: Subscription;     public source$: Subject&lt;DataType&gt;;               onBChange(newB: TypeOfB) {      this.subscriptionToC.unsubscribe();         this.subscriptionToC = newB.C.subscribe(          source$.next         );     } } <\/code><\/pre>\n<p>\u8fd9\u6837\u5f53\u7136\u80fd\u591f\u89e3\u51b3\u95ee\u9898\uff0c\u4f46\u662f\u611f\u89c9\u5f88\u810f\uff0c\u56e0\u4e3a\u5f15\u5165\u4e86\u4e00\u4e2a\u591a\u4f59\u7684 subscription\uff0c\u9700\u8981\u5728 A \u7684\u751f\u547d\u5468\u671f\u5185\u8fdb\u884c\u989d\u5916\u7684\u7ba1\u7406\u64cd\u4f5c\uff0c\u5e76\u4e14\u9700\u8981\u624b\u52a8\u5730\u5173\u6ce8 B \u4ec0\u4e48\u65f6\u5019\u4f1a\u53d1\u751f\u53d8\u5316\u3002<\/p>\n<p>\u7531\u4e8e\u4e0d\u7cbe\u901a RxJS\uff0c\u4e0d\u77e5\u9053\u80fd\u4e0d\u80fd\u901a\u8fc7\u4ec0\u4e48 operator \u5b9e\u73b0\u8fd9\u4e2a\u76ee\u6807\uff0c\u5e0c\u671b\u5927\u5bb6\u5e2e\u5fd9\u89e3\u7b54\uff5e<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>4<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"3690056\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : gdtdpt <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u6211\u611f\u89c9\u5e94\u8be5\u662f\u4f60\u9700\u8981\u4e00\u4e2a Observable&lt;B&gt;\u7136\u540e\u518d\u7531 B -&gt; flatMap \u5230 C\uff0c\u8fd9\u6837 B \u6539\u53d8\u540e\u6d41\u4e5f\u4f1a\u6267\u884c\u5f53\u524d B \u4e2d\u7684 C \u3002RxJS \u597d\u4e45\u4e0d\u7528\u4e86\uff0c\u53ef\u80fd\u4e0d\u592a\u5bf9\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3690057\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : cuzfinal <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u628a C \u8bbe\u7f6e\u6210\u4e00\u4e2a BehaviorSubject\uff0c\u7136\u540e\u5728 A \u7684\u5185\u90e8\u5904\u7406 B \u7684 subscribe \u548c unsubscribe\uff0c\u6bcf\u6b21\u63a5\u6536\u5230\u65b0\u7684\u6570\u636e\u65f6\u76f4\u63a5 c.next(data)\uff0cC \u4e00\u76f4\u4e0d\u53d8\uff0c\u8fd9\u6837\u5c31\u80fd\u76f4\u63a5\u628a C \u66b4\u9732\u7ed9\u7528\u6237\u4e86\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3690058\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : faceRollingKB <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u56e0\u4e3a\u4f60\u7684 B \u4e5f\u662f\u52a8\u6001\u6539\u53d8\u7684\uff0c\u6240\u4ee5\u4e0d\u624b\u52a8 unsubscribe+ subscribe \u7684\u65b9\u6848\u9700\u8981\u4f60\u628a B \u8f6c\u5316\u4e3a observable B$\uff0c\u7136\u540e\u4ece\u5916\u90e8\u8ba2\u9605 B$.pipe(switchMap(B =&gt; B.C))\u624d\u884c\uff0c\u5f53\u7136\u53ea\u662f\u60f3\u6cd5\u6211\u6ca1\u8bd5\u8fc7                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3690059\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : akatquas <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             https:\/\/rxjs-dev.firebaseapp.com\/api\/index\/class\/Subject#asObservable<\/p>\n<p>Observable \u5b58\u5728\u8fd9\u4e2a API \u6765\u540c\u6e90\u521b\u5efa\u4e00\u4e2a\u65b0\u7684 observable\uff0c\u503c\u5f97\u4e00\u8bd5                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u95ee\u4e00\u4e2a\u5173\u4e8e RxJS \u7684\u6280\u672f\u95ee\u9898 &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/167074"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=167074"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/167074\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=167074"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=167074"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=167074"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}