Posted 鍓嶇琛ョ粰绔?/a> 鍝嶅
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了相关的知识,希望对你有一定的参考价值。
锛堭煈嗙偣鍑?/span>鍓嶇琛ョ粰绔?/span>锛屽彲蹇€熷叧娉級
鎹姣忎釜绋嬪簭鍛樼瓑鐢垫鐨勬椂鍊欓兘鎬濊€冭繃鐢垫鐨勮皟搴︾畻娉曗€︽墍浠ユ€庝箞鍔ㄦ墜瀹炵幇涓€涓憿锛熻櫧鐒惰繖涓満鏅矊浼兼湁浜涘鏉傦紝浣嗗嵈闈炲父閫傚悎浣跨敤鍝嶅簲寮忕紪绋嬬殑鑼冨紡鏉ュ鐞嗐€備笅闈㈡垜浠細鍦?RxJS 鍜?Vue 鐨勫熀纭€涓婏紝涓€姝ユ瀹炵幇鍑轰竴涓渶灏忓彲鐢ㄧ殑鐢垫璋冨害妯℃嫙 Demo銆?/p>
Demo
涓轰簡閬垮厤璇昏€呫€愯劚浜嗚¥瀛愬氨缁欐垜鐪嬭繖涓紵銆戠殑鍚愭Ы锛屽湪姝ゆ垜浠厛灞曠ず 50 琛屼唬鐮佹渶缁堟墍鑳藉疄鐜扮殑鏁堟灉锛氫竴鍙?10 灞傛ゼ鐨勭數姊紝浣犲彲浠ュ湪姣忓眰妤兼寜 閾炬帴 demo锛?/span> https://ewind.us/h5/rx/final.html 寰俊涓婄殑 https://ewind.us/2017/rx-elevator-demo/ 鍦ㄤ粙缁嶅疄闄呯殑缂栫爜缁嗚妭鍓嶏紝鎴戜滑涓嶅Θ鍏堣€冭檻娓呮鏈€鍩虹鐨勬€濊矾锛屽嵆濡備綍琛ㄨ揪鐢垫鐨勮皟搴︼紵鎴栬€呮崲涓€绉嶈〃杩版柟寮忥紝杩欏叾瀹炴槸涓洿涓烘湁瓒g殑璇濋锛?span class="mq-31">濡備綍浣跨敤浠g爜鎶借薄鍑轰竴鍙扮數姊憿锛?/span> 涔熻楂樹腑鐗╃悊瀛﹀緱濂界殑鍚屽棣栧厛浼氳繖涔堟兂锛氱數姊彲浠ユ娊璞℃垚鐢变竴鏉$怀瀛愭寕鐫€鐨勭洅瀛愶紝鎴戜滑鍙互浼犲叆瀹冪殑閲嶉噺 m銆佺鍦伴珮搴?h銆佸綋鍓嶉€熷害 v銆佸綋鍓嶅姞閫熷害 a锛岀劧鍚庣敤涓€绯诲垪绮惧鐨勫叕寮忔潵鎻忚堪瀹冪殑杩愬姩杞ㄨ抗鈥︹€︽伃鍠滀綘锛岀悊绉戞€濈淮鎶婁綘寮曞叆姝ч€斾簡馃檮璇锋斁蹇冿紝鏈€鍚庣殑 50 琛屼唬鐮侀噷涓嶆秹鍙婁换浣曢珮涓墿鐞嗙煡璇?/span>銆?/p>
鍊掓槸鏈変釜鍏充簬鐢垫鐨勮€佹瀛愭洿绗﹀悎鎴戜滑鐨勬娊璞★細銆愪竴涓€佸睂涓濈湅鍒颁竴涓€佸お濠嗚繘浜嗙數姊棿锛屼竴浼氬嚭鏉ョ殑灞呯劧鏄釜鐧藉瘜缇庯紝浜庢槸灏辨兂鐫€瑕佹槸甯︿簡鑷繁鐨勮€佸﹩鏉ヨ澶氬ソ鍟娾€︹€︺€戣繖閲屽鐢垫鐨勬娊璞★紝鍙笉杩囨槸涓€鎵囨暟瀛椾細璺冲姩鐨勯棬鑰屽凡銆傛垜浠笉闇€瑕佸叧蹇冨畠鐨勬満姊板埌搴曟€庢牱杩愪綔锛屽浜庡畠鐨?span class="mq-39">鐘舵€?/span>锛屽彧瑕佺煡閬撶數姊彛娑叉櫠灞忎笂鐨?span class="mq-40">鏂瑰悜 杩欎袱绉嶆€濈淮鏈変粈涔堝尯鍒憿锛熻鎴戜滑鏉ヨ€冭檻鏈€绠€鍗曠殑鎯呭舰锛氬湪鍗佹ゼ鎸変竴涓敭锛屾妸鐢垫浠庝竴妤煎彨涓婃潵銆傝繖鏃讹紝涓ょ鎶借薄鏂规硶鎵€鎻忚堪鐨勫唴瀹逛細鏈夊緢澶х殑涓嶅悓锛?/p>
娉曚竴锛氱洅瀛愬紑濮嬩互閫熷害 v 鍚戜笂杩愬姩锛屽湪鍗佹ゼ鐨勯珮搴?h 鍋滀笅鏉ャ€?/p> 娉曚簩锛氭ゼ灞傛暟瀛椾粠 1 寮€濮嬶紝鎸夊浐瀹氭椂闂撮棿闅斿姞涓€锛屽埌 10 鍋滄銆?/p> 鍡紝鐪嬭捣鏉ュ悗鑰呭疄鐜拌捣鏉ュ緢绠€鍗曞晩锛氬彧瑕佹瘡闅斾竴绉?nbsp; 浣犲湪浜屾ゼ鎯充笅妤硷紝鍙戠幇鐢垫姝d粠涓夋ゼ涓嬫潵銆傝繖鏃跺€欑數姊細鎹庝笂浣狆煒?/p> 浣犲湪鍗佹ゼ鎯充笅妤硷紝鍙戠幇鐢垫姝e湪涔濇ゼ寰€涓嬭蛋銆傝繖鏃跺€欑數姊苟涓嶄細鍥炲ご鏉ユ帴浣狆煒?/p> 浣犲湪鍗佹ゼ鎯充笅妤硷紝鍙戠幇鐢垫姝d粠浜屾ゼ涓婃潵銆備綘浠ヤ负瀹冧細鍋滃湪浣犺繖锛岀粨鏋滃叾瀹炴槸浜屽崄妤肩殑娣疯泲鍙殑鐢垫馃槨 鈥︹€?/p> 濂界殑锛岃繖鏃跺€?nbsp; 鍦?Reactive 鑼冨紡涓紝Stream 浜嬩欢娴佺殑姒傚康闈炲父寮哄ぇ銆傛垜浠兘鐭ラ亾璁$畻鏈哄鐞嗙殑鏁版嵁鏈川涓婇兘鏄鏁g殑锛屽嵆渚挎槸灏忓濮愮殑瑙嗛锛屼篃瑕佹媶鎴愪竴绉?24 甯с€傚浜庢垜浠殑鐢垫妯℃嫙鍣紝瀹冪殑杈撳叆鍏跺疄灏辨槸鐢ㄦ埛鍦ㄥ悇涓ゼ灞備笂闅忔椂闂村彉鍖栫殑涓€绯诲垪绂绘暎鎿嶄綔锛岃緭鍑哄垯鏄竴涓綋鍓嶆椂闂存ゼ灞傚拰鏂瑰悜鐨勭姸鎬?/span>銆傝繖鏍凤紝鎴戜滑灏辫兘澶熶娇鐢?Stream 鏉ヨ〃杈炬ā鎷熷櫒鐨勮緭鍏ヤ簡銆?/p>
Stream 鍜屾湸绱犵殑浜嬩欢鐩戝惉鍣ㄦ湁浠€涔堝尯鍒憿锛烻tream 鏄彲浠?span class="mq-61">鍦ㄦ椂闂寸淮搴︿笂杩涜缁勫悎銆佺瓫閫夌瓑鍙樻崲鐨?/span>銆傚鏋滆寰楄繖涓娉曞緢鎶借薄锛屼笉濡ㄨ€冭檻杩欎釜渚嬪瓙锛氬湪鍗佹ゼ鎸変竴娆$數姊寜閽紝妤煎眰鏁板瓧浼氫粠 1 閫愪釜璧板埌 10銆傝繖鏃讹紝鎴戜滑灏辨妸涓€涓簨浠舵祦涓殑涓€涓簨浠讹紝鏄犲皠涓轰簡涓€涓緷娆¤Е鍙戝崄娆′簨浠剁殑鏂版祦銆傚啀姣斿锛屾垜浠彧瑕佹妸浠庝竴妤煎埌鍗佹ゼ鐨勪簨浠舵祦鍜?span class="mq-64">浠庡崄妤煎埌涓€妤?/span>鐨勪簨浠舵祦绠€鍗曞湴杩炴帴璧锋潵锛屽氨瀹炵幇浜?span class="mq-66">涓婃ゼ鎺ヤ汉鍐嶈繑鍥?/span>鐨勭數姊熀鏈姛鑳斤紒 璇濋兘璇村埌杩欎唤涓婁簡锛屼篃宸笉澶氭槸鏃跺€?Show Me the Code 浜嗮煠撲笅闈㈣鎴戜滑鏉ヤ竴姝ユ浣跨敤 Reactive 瀹炵幇 Demo 鍚с€?/p>
鍏堢畝瑕佷粙缁嶄竴涓嬭繖涓?Demo 鐨勬妧鏈儗鏅細涓虹畝鍗曡捣瑙侊紝鎴戜滑閫夋嫨浜?Vue 鏉ュ厖褰撶畝鍗曠殑瑙嗗浘灞傦紝閫夋嫨浜?nbsp;RxJS 杩欎釜 Reactive 搴撴潵瀹炵幇鏍稿績鐨勫姛鑳姐€傚彈闄愪簬绡囧箙锛屾垜浠笉浼氳鐩?Vue 鐨勪娇鐢ㄧ粏鑺傦紝鍙粙缁?Reactive 鐩稿叧鐨勯噸瑕佺壒鎬ю煓冨彟涓€鏂归潰锛屼粠 0 鍒?1 鎬绘槸鏈€闅剧殑锛屽洜姝?Step 1 鐨勫唴瀹逛篃浼氭槸鏈€澶氱殑馃槄 涓婃枃涓紝鎴戜滑宸茬粡鎻愬埌浜?Rx 涓祦鐨勫己澶с€傞偅涔堬紝鎴戜滑棣栧厛鑰冭檻杩欎釜鏈€鏈€鍩烘湰鐨勯渶姹傚惂锛?span class="mq-76">鍦ㄥ崄妤兼寜涓€涓?nbsp; 鎵ц涓婇潰鐨勪唬鐮侊紝鐐瑰嚮鎸夐挳鏃讹紝灏变細姣忕瑙﹀彂涓€涓粠 0 寮€濮嬭嚜澧炵殑浜嬩欢娴佷簡锛屾瘡绉掍篃閮借兘鍦ㄦ帶鍒跺彴鐪嬪埌绋冲畾鐨勮緭鍑恒€備絾杩欏苟涓嶇鍚堣姹傦細鎬庢牱璁╂ゼ灞傚彧澧炲姞鍗佹鍛紵鎴戜滑寮曞叆 鍡紝鎺ヤ笅鏉ワ紝鎴戜滑鍙戠幇杩樻湁涓€鐐逛笉澶紭闆咃細妤煎眰鏁板瓧铏界劧鎸夎姹傞€掑浜嗭紝浣嗗嵈鏄粠 0 鍒?9锛岃€岄潪浠?1 鍒?10锛堜綘瀹舵湁 0 灞傚悧锛燂級瑕佹寜鐓х壒瀹氳鍒欐槧灏勫嚭鏂版祦锛屾垜浠洿鎺ヤ娇鐢ㄧ啛鎮夌殑 鐜板湪鎴戜滑鑳藉浠庝竴妤煎埌鍗佹ゼ浜嗭紝浣嗘槸鎬庝箞涓嬫ゼ鍛紵鎴戜滑鍏堥€犱竴涓粠鍗佹ゼ鍒颁竴妤肩殑 Stream 鍚ю煒?/p>
鐢垫闇€瑕佸厛 UP 涓婃ゼ锛屽啀 DOWN 涓嬫ゼ銆備负姝わ紝鎴戜滑鐩存帴 鐩墠鎴戜滑宸茬粡浣跨敤浜?nbsp; 浠庤繖鍑犱釜 API 鐨勪娇鐢ㄤ笂锛屾湁浜涢€兼牸姣旇緝楂樼殑鍚屽涔熻浼氬彂鐜帮紝鎴戜滑鐨勭紪鐮佺畻娉曪紝鍏跺疄鏈変簺鎺ヨ繎鎷夋櫘鎷夋柉鐨?span class="mq-144">鍐冲畾璁?/span>锛氱數姊殑鎸夐挳琚寜涓嬪悗锛屽畠鍦?span class="mq-145">鏈潵涓€娈垫椂闂村唴鐨勪竴绯诲垪鐘舵€佸彉鍖栧湪閭d竴涓椂鍒诲氨宸茬粡琚喅瀹氫簡銆傛崲鍙ヨ瘽璇达紝缁欐垜涓€涓冻澶熺簿纭殑褰撳墠鐘舵€侊紝鎴戣兘璁$畻鍑烘暣涓湭鏉ワ紙琚嫋璧帮級鈥︹€﹁繖鏃跺€欐垜浠鍏堥亣鍒扮殑楹荤儲鏄細濡傛灉鍦ㄨ緭鍑虹殑涓€绯诲垪浜嬩欢鎵ц鏃堕棿涓紝鍙堝嚭鐜颁簡鏂扮殑杈撳叆浜嬩欢锛岃濡備綍瀹氫箟鍚庣画鐨勭姸鎬佸憿锛?/span> 杩欓噷锛屾垜浠紩鍏ヤ簡 閾炬帴 step 1锛?/span> https://ewind.us/h5/rx/step1.html 鍦ㄤ笂闈㈢殑 Demo 涓偣鍑讳换浣曚竴涓寜閽紝鐢垫灏变細浠庝竴妤煎紑濮嬪幓鎺ヤ綘锛岀劧鍚庤繑鍥炪€備腑閫斿鏋滃啀娆$偣鍑绘柊妤煎眰锛岀數姊氨浼氱珛鍒婚噸鏂颁粠涓€妤煎嚭鍙戯紙閲忓瓙鍖栵紵锛夊幓鏂版ゼ灞傛帴浜恒€傚棷绂诲疄鐢ㄨ繕鏈夋璺濈锛屼笉杩囧凡缁忔湁涓牱瀛愬暒銆傝€岀洰鍓嶆垜浠殑 Rx 閫昏緫澶ф闀胯繖鏍凤紝闈炲父绠€鐭細 杩欎竴姝ヤ腑锛屾垜浠渶瑕佽В鍐崇數姊湪鏂版寜閽寜涓嬫椂锛岀濂囧湴閲忓瓙鍖栧嚭鐜板湪涓€妤肩殑闂锛堣锛夈€傛垜浠笉闇€瑕佸紩鍏ユ柊鐨?API锛屽彧闇€瑕佺◢寰慨姝d竴涓嬮€昏緫锛?/p>
绗竴姝ヤ腑锛屾垜浠緭鍏ユ祦涓殑鐘舵€佸彧鏈?nbsp; 澧炲姞杩欎釜鐘舵€佸悗锛孲tep 2 鐨勬晥鏋滃涓嬫墍绀猴細 閾炬帴 step 2锛?/span> https://ewind.us/h5/rx/step2.html 杩欎釜 Demo 閲岋紝浣犲彲浠ュ厛鐐瑰嚮浜旀ゼ锛岀瓑鍒扮數姊蛋鍒颁笁妤兼椂鍐嶇偣鍑讳竷妤笺€傝繖鏃剁數姊笉浼氱洿鎺ュ嚭鐜板湪涓€妤硷紝鑰屾槸浼氫粠涓夋ゼ鑰佽€佸疄瀹炲湴鐖笂涓冩ゼ鍐嶄笅鏉ャ€?/p>
涓嶈繃杩欏氨甯︽潵浜嗘柊鐨勭姸鎬侀棶棰橈細鍏堢偣鍑讳簲妤硷紝绛夌數姊蛋鍒颁笁妤兼椂鐐瑰嚮浜屾ゼ銆侭oom锛佺數姊嚭 bug 璧颁笉鍔ㄤ簡鈥︹€?/p>
涓婁竴姝ョ殑 bug 鍑虹幇鍘熷洜锛屾槸浣?nbsp; 濂戒簡锛宐ug 淇浜嗭細 step 3锛?/span> http://ewind.us/h5/rx/step3.html 涓婇潰鐨勪緥瀛愪腑锛屼笉绠℃€庝箞鎸夋寜閽紝鐢垫缁堜簬閮戒笉浼氶噺瀛愬寲锛屼篃閮戒笉浼氳鐜╁潖鍟︼紒浣嗘槸鏂扮殑椋庢毚鍙堝嚭鐜颁簡锛氭潵鍥炵偣鍗佹ゼ鍜屼簲妤硷紝浼氬彂鐜颁负浠€涔堣繖涓數姊潵鏉ュ幓鍘诲嵈鎬绘槸鍒颁笉浜嗕竴妤煎憿鈥︹€?/p>
鍦ㄤ笂闈㈢殑渚嬪瓙涓紝鎴戜滑浼犲叆 Stream 鐨勭姸鎬佸叾瀹炲缁堜笉瓒充互鏀拺鐢垫璋冨害绠楁硶鐨勬甯稿伐浣溿€傛瘮濡傦紝鎴戜滑骞舵病鏈夋爣蹇楀嚭涓€涓ゼ灞傛湁娌℃湁琚寜閽偣浜€傚湪杩欎竴姝ヤ腑锛屾垜浠湪 Vue 鐨勮鍥惧眰澧炲姞涓€涓繖鏍风殑鐘舵€侊細 鍡笉瑕佸湪鎰忔垜浠病鏈?nbsp; 鎬讳箣鐜板湪妯℃嫙鍣ㄧ湅璧锋潵闀胯繖鏍凤細 閾炬帴 step 4锛?/span> https://ewind.us/h5/rx/step4.html 鐐瑰嚮鏃朵細鍦?Rx 涓脊鍑轰竴涓啋鐩殑 鍦ㄦ渶鍚庝竴姝ラ噷锛屾垜浠渶瑕佷娇鐢?Rx 澶勭悊涔嬪墠鍒颁笉浜嗕竴妤肩殑闂銆傛垜浠煡閬擄紝鏍规嵁銆愬喅瀹氳銆戠殑鎬濇兂锛孯x 鍏跺疄鍦ㄦ瘡涓寜閽簨浠惰Е鍙戞椂锛屽氨宸茬粡瑙勫垝濂戒簡鏈潵鐨勭數姊繍鍔ㄤ簡銆傞偅涔堬紝鎴戜滑鑳戒笉鑳藉仛鍋氬噺娉曪紝鎶婂奖鍝嶇姸鎬佺殑浜嬩欢杩囨护鎺夊憿锛熻繖閲屾垜浠彲浠ヤ娇鐢?nbsp; 绠€鍖栫殑妯″瀷涓紝鎴戜滑涓嶅Θ璁や负鐢垫鍙細鎵ц銆愬厛 up 鍐?down銆戠殑鎿嶄綔銆傝繖鏃讹紝瀵逛簬鐢垫杩愬姩杩囩▼涓Е鍙戠殑鏂颁簨浠讹紝鍙互杩欐牱鍒嗙被锛?/p>
濡傛灉鐢垫姝e湪涓嬮檷锛岄偅涔堜笉绠″湪鍝釜妤煎眰瑙﹀彂鐨勬柊浜嬩欢閮戒笉鑳藉啀娆¤鐢垫鍐嶆 up and down锛屼繚璇佺數姊€昏兘涓嬮檷鍒颁竴妤?/p> 濡傛灉鐢垫姝e湪涓婂崌锛屼絾鏄柊鐨勪笅闄嶄簨浠舵墍鍦ㄦゼ灞備綆浜庡綋鍓嶆ゼ灞傦紝閭d箞鐢垫鍦ㄨ繖涓€杞笅闄嶈繃绋嬩腑灏卞彲浠ョ粡杩囪繖涓柊妤煎眰锛屼粠鑰屼笉闇€瑕佸啀娆?up and down 濡傛灉鐢垫姝e湪涓婂崌锛岃€屼笖鏂扮殑涓嬮檷鏃堕棿鎵€鍦ㄦゼ灞傞珮浜庡綋鍓嶆ゼ灞傦紝閭d箞鎴戜滑閲嶆柊杩涜涓€娆$洰鏍囦负鏂版ゼ灞傜殑 up and down 鍗冲彲銆?/p> 涓夌鎯呭舰涓紝鎴戜滑浼氬垽鏂嚭鏄惁闇€瑕?up and down銆傛棦鐒舵瘡娆?up and down 閮芥槸杈撳叆 鍦ㄦ斁缃繖涓€昏緫鍚庯紝鎴戜滑鎶?up and down 鐨勭洰鏍囨ゼ灞傜敱浜嬩欢鎵€鍦ㄦゼ灞傦紝鏀逛负浠?floors 涓壘鍑虹殑鏈€楂樻ゼ灞傦紙maxTargetFloor锛夛紝灏辫兘澶熶繚璇佺數姊甯告姷杈剧洰鏍囨ゼ灞傚苟姝e父杩斿洖浜嗐€備笉杩囪繖鏃惰繕鏈夋渶鍚庣殑涓€鐐瑰皬闂锛氬鏋滅數姊笅闄嶄腑浣犳寜涓嬩簡鍗佹ゼ锛岄偅涔堢數姊埌杈句竴妤煎悗涓嶄細鍐嶆鏉ユ帴浣犫€﹁В鍐虫柟娉曞緢绠€鍗曪紝鍦ㄧ數姊笅闄嶅埌杈句竴灞傛椂锛屽皾璇曡鐢垫鍐?up and down 涓€娆″嵆鍙€?/p>
鍦ㄦ垜浠疄鐜板畬浜嗘渶鍚庣殑杩欎竴鐐瑰紓姝ラ€昏緫鍚庯紝灏辨槸鏈枃寮€濮嬫椂鐨?Demo 浜嗭細 閾炬帴 final锛?/span> https://ewind.us/h5/rx/final.html 鍒拌繖鏃讹紝Rx 涓殑浠g爜浠嶇劧浠呮湁 40 浣欒銆傝€?Vue 涓殑浠g爜涔熸病鏈夋秹鍙婁换浣曠殑寮傛閫昏緫锛屼粎浠呴渶瑕佸 Observable 鍋氱畝鍗曠殑璁㈤槄骞舵覆鏌撴暟鎹嵆鍙€?/p>
鐩墠涓烘锛屾垜浠殑妯℃嫙鍣ㄥ姛鑳藉叾瀹炶繕鍙槸鐪熸鐢垫鐨勪竴涓瓙闆嗭紝瀹冭繕缂哄皯杩欐牱鐨勫姛鑳斤細 涓€涓鐢ㄦ埛鍦ㄧ數姊噷閫夋嫨鐘舵€佺殑闈㈡澘 姣忓眰鐨?nbsp; 涓嶈繃鍦?Rx 鐨勫熀鏈€濊矾鍩虹涓婏紝妯℃嫙鍑鸿繖浜涚壒鎬у苟涓嶄細鏄捐憲鍦板鍔犲鏉傚害锛氬湪鐢垫閲岄€夋嫨鐘舵€佹墍瑙﹀彂鐨勪簨浠讹紝鍏跺疄鍦ㄤ紭鍏堢骇涓婂畬鍏ㄧ瓑鏁堜簬鍦ㄧ數姊棬澶栫殑妤煎眰閫夋嫨锛堝湪鍚戜笂杩愯鐨勭數姊唴鎸変竴妤硷紝鐢垫涓嶄細鐞嗕綘锛屽氨鑳藉璇佹槑杩欎竴鐐癸級锛涜€屽紩鍏?nbsp; 濡傛灉浣犺繕鍦ㄧ籂缁撻渶涓嶉渶瑕佸湪宸叉湁椤圭洰涓紩鍏?Rx锛屼篃璁告湰鏂囩殑瀹炶返鑳藉涓轰綘鎻愪緵涓€浜涘皬鍙傝€冿細Rx 鍦ㄥ鐞嗗紓姝ヤ簨浠舵祦鏃堕潪甯稿己澶э紝绫讳技Redux / MobX 绛夌姸鎬佺鐞嗗櫒鎵€鍏虫敞鐨勪笌 Rx 鍏跺疄骞堕潪鍚屼釜灞傞潰鐨勯棶棰橈紝涓€鏃﹀皢瀹冧滑涓?Rx 缁撳悎锛屾槸鑳藉澶勭悊寰堥珮鐨勪笟鍔″鏉傚害鐨勩€?/p>
鏈€鍚庯紝杩欏叾瀹炰綔鑰呯涓€娆″皾璇?Rx 鐨勯」鐩€傜湡姝g紪鍐欑殑浠g爜骞朵笉澶氾紝涓嶈繃瑕侀€傚簲瀹冨苟浣跨敤瀹冪湡姝hВ鍐抽棶棰橈紝鎵€闇€瑕佺殑鎬濊€冩椂闂村叾瀹炴瘮鏁查敭鐩樺啓鍑犺浠g爜鐨勬椂闂磋澶氬緱澶氣€︹€﹁繖涔熺畻鏄竴绉嶄箰瓒e惂馃檭鏈枃涓瘡涓€涓?Step 閮芥槸浠庡紑鍙戣繃绋嬩腑鐨勭湡瀹?commit 鎶藉彇鍑烘潵鐨勶紝甯屾湜鏈枃瀵瑰ぇ瀹舵湁鎵€甯姪馃檭 Github 浼犻€侀棬锛?/span> https://github.com/doodlewind/rx-elevator-demo Observable 鏂囨。锛?/span> http://reactivex.io/rxjs/class/es6/Observable.js~Observable.html /閭€鎮ㄤ竴鍚屽涔? /鏇村闃呰/ 鐑?/span> 馃敟 鐑?/span> 馃敟 鐑?/span> 馃敟 鐑?/span> 馃敟
瑙夊緱鏈枃寰堟湁甯姪锛熻浼犻€掔粰鏇村鏈嬪弸 鍏虫敞鈥?nbsp;鍓嶇琛ョ粰绔欌€?nbsp;锛?/span>鎻愬崌鍓嶇鎶€鑳?/span> 以上是关于的主要内容,如果未能解决你的问题,请参考以下文章鈫?/code> 鍙敜鐢垫鎶婁綘閫佸埌涓€妤笺€傚湪澶氫釜妤煎眰鏍规嵁涓嶅悓鏃跺簭鍙敜鍑虹數姊殑鏃跺€欙紝杩欎釜妯℃嫙鍣ㄧ殑鍗囬檷鐘舵€佸簲褰撴槸鍜屾棩甯哥殑浣撻獙涓€鑷寸殑銆傚厛鍒€ョ潃鍚愭Ы瀹冧负浠€涔堣繖涔堢畝闄嬶紝鎶婂畠瀹炵幇鎴愯繖鏍风殑鐞嗙敱浼氬湪涓嬫枃涓參鎱粙缁嶐煒?/p>
iframe
鏍囩涓嶈兘姝e父宸ヤ綔锛孌emo 涓嶅Θ鏌ラ槄 Blog PostGet Started
setTimeout
鏀逛竴涓嬫ゼ灞傛暟锛岃繖涓數姊氨妯℃嫙鍑烘潵鍟︷煒庢伃鍠滀綘锛屼綘璺宠繘浜嗗紓姝ヤ簨浠舵祦鐨勫ぇ鍧戦噷锛岃€冭檻杩欎簺闇€姹傦細
setTimeout
鎭愭€曚笉澶熺敤鍟︼紝鑷充簬浠€涔?Redux Flux MobX鈥︹€﹀啓杩欑闇€姹備篃瑕佹帀灞傜毊銆傚棷锛屽埌姝ゆ垜浠殑鍓嶆垙缁堜簬宸笉澶氫簡锛屾槸鏃跺€欎粙缁嶆湰鏂囩殑涓昏 Reactive Programming 鍝嶅簲寮忕紪绋嬩簡馃榾Step 1
鈫?/code>锛岀數姊暟瀛椾粠 1 寮€濮嬮€愭閫掑
import { Observable } from 'rxjs'const stream = Observable // 灏?DOM 鐨勬ゼ灞傜偣鍑讳簨浠惰浆鍖栦负 Observable 浜嬩欢娴?/span>
.fromEvent(emitter, 'click') // 杈撳叆浜嬩欢娴侊紝杈撳嚭闂撮殧 1s 瑙﹀彂鏂颁簨浠剁殑鏂版祦
.interval(1000)// 娴佺殑涓€绯诲垪寮傛杈撳嚭鍙互琚闃?/span>stream.subscribe(x => console.log(x))
take
鏂规硶锛?/p>
const up = Observable
.fromEvent(emitter, 'click')
.interval(1000) // 鍙細瑙﹀彂鍗佹锛?/span>
.take(10)
map
鏂规硶灏辫锛?/p>
const up = Observable
.fromEvent(emitter, 'click')
.interval(1000)
.take(10) // +1 馃惛
.map(x => x + 1)
const down = Observable
.interval(1000)
.map(x => 10 - x)
.take(10)
concat
涓や釜 Stream 灏辫锛?/p>
function getStream () { // 澹版槑 Up 鍜?Down...
return up.concat(down)
}
interval
/ take
/ map
/ concat
杩欏嚑涓?API 浜嗭紝涓嶈繃绂荤湡姝e畬鎴?Step 1 杩欎竴姝ワ紝杩樻湁涓€涓潪甯稿叧閿殑鍦版柟锛?span class="mq-140">鍦ㄤ笉鍚屾ゼ灞傚娆℃寜涓嬬數姊寜閽椂锛屽浣曟帶鍒朵簨浠舵祦锛?/span>switchMap
鏂规硶鏉ヨ〃杈鹃€昏緫锛氬亣璁惧湪鍗佹ゼ鎸変笅鎸夐挳锛屽湪鏈潵鐨勫崄绉掍細瑙﹀彂鍗佷釜浜嬩欢銆傞偅涔堢粡杩?nbsp;switchMap
鐨勫皝瑁咃紝涓€鏃﹀湪鍗佺涓殑鏌愪釜鏃跺埢鍙堟湁鏂版寜閽鎸変笅锛屽師鍏堝墿浣欑殑浜嬩欢灏辫鑸嶅純锛屼粠杩欐椂璧锋敼涓鸿Е鍙戞柊鎸夐挳浜嬩欢琛嶇敓鍑虹殑鏂颁簨浠躲€?/span>鎹竴绉嶈娉曪紝灏辨槸浠庝竴妤煎埌鍗佹ゼ鐨勭數姊紝濡傛灉璧板埌涓€鍗婃湁浜烘寜浜嗕簲妤硷紝灏辩珛鍒讳粠涓€妤奸噸鏂板嚭鍙戯紝璧板埌浜旀ゼ杩斿洖銆傛棦鐒舵垜浠彧鍏冲績鐘舵€侊紝涓嶅叧蹇冭繖涔堥噺瀛愬寲鐨勭數姊埌搴曟€庝箞瀹炵幇鐨勶紝杩欎釜 Step 1 鐨勬ā鎷熷櫒鎵ц缁撴灉鍊掍篃鏄ǔ瀹氱殑銆傜◢寰皝瑁呭嚭涓€浜涘弬鏁帮紝绗竴涓?Demo 灏卞畬鎴愬暒锛?/p>
import { Observable } from 'rxjs'export function getStream (emitter, type) { return Observable
.fromEvent(emitter, type) // target 涓?Vue 涓Е鍙戞寜閽簨浠剁殑妤煎眰鍙?/span>
.switchMap(({ target }) => { const up = Observable
.interval(1000)
.map(x => x + 1)
.take(target) const down = Observable
.interval(1000)
.map(x => target - x)
.take(target) return up.concat(down)
})
}
Step 2
target
杩欎釜鍞竴鐨勭洰鏍囨ゼ灞傦紝杩欏氨鎰忓懗鐫€鐢垫鐢氳嚦涓嶇煡閬撴寜閽Е鍙戞椂锛岃嚜宸卞綋鍓嶆鍦ㄥ嚑妤?/span>銆備负姝わ紝鎴戜滑鍦?Vue 涓坊鍔犱竴涓?nbsp;curr
鍙傛暟鏉ユ爣璁拌繖涓姸鎬侊紝杩欐牱锛岀數姊瘡褰撴柊浜嬩欢瑙﹀彂鏃讹紝灏变細浠庡綋鍓嶆ゼ灞傚幓寰€鏂扮洰鏍囨ゼ灞傦紝鑰屼笉鏄洿鎺ュ嚭鐜板湪涓€妤硷細// 澧炲姞涓€涓?curr 鍙傛暟.switchMap(({ target, curr }) => { const up = Observable
.interval(1000) // 浠庡綋鍓嶆ゼ灞傚嚭鍙戝幓寰€鏂版ゼ灞?/span>
.map(x => x + curr)
.take(target + 1 - curr) const down = Observable
.interval(1000)
.map(x => target - x)
.take(target) return up.concat(down)
Step 3
take
浜嗕竴涓礋鏁帮紙鏈潵浠庝簲妤煎埌鍏ゼ闇€瑕?nbsp;take
涓€娆★紝浣嗕粠浜旀ゼ鍒板洓妤煎垯鏄?nbsp;take
-1 娆★級銆傛櫘閫氱殑鏁扮粍涓嬫爣瓒婄晫鍊掕繕濂斤紝闈㈠悜鏃堕棿搴忓垪鐨?Observable 涓嬫爣瓒婄晫鐨勮瘽锛岄偅鍙氨鏄湡姝g殑 -1s
浜嗏€︹€︽垜浠潵琛ヤ竴鐐归€昏緫淇瀹冨惂锛?/p>
.switchMap(({ target, curr }) => { // 鐩爣妤煎眰楂樹簬褰撳墠妤煎眰锛屾垜浠厛涓婃ゼ鍐嶄笅妤?/span>
if (target >= curr) { const up = Observable
.interval(1000)
.map(x => x + curr)
.take(target + 1 - curr) const down = Observable
.interval(1000)
.map(x => target - x)
.take(target) return up.concat(down)
} else { // 鐩爣妤煎眰浣庝簬褰撳墠妤煎眰锛屾垜浠洿鎺ヤ笅妤?/span>
return Observable
.interval(1000)
.map(x => curr - x)
.take(curr)
}
Step 4
// ...
data () { return { floors: [
{ up: false, down: false },
{ up: false, down: false },
{ up: false, down: false },
{ up: false, down: false },
{ up: false, down: false },
{ up: false, down: false },
{ up: false, down: false },
{ up: false, down: false },
{ up: false, down: false },
{ up: false, down: false }
], currFloor: 1
}
},
鈫?/code> 鎸夐挳涓轰粈涔堟湁
up
鐘舵€佽繖浜涚粏鑺備簡銆傝€?Rx 涓垜浠坊鍔犱竴浜涚畝鍗曠殑澶勭悊锛岃浜嬩欢娴佷紶鍑虹殑鐘舵€佷笉浠呬粎鍖呮嫭褰撳墠妤煎眰锛屼篃鍖呮嫭褰撳墠鏂瑰悜锛?/p>
if (targetFloor >= baseFloor) { const up = Observable
.interval(1000)
.map(count => { const newFloor = count + baseFloor return { floor: newFloor, // 浼犲嚭褰撳墠鏂瑰悜
direction: newFloor === targetFloor ? 'stop' : 'up'
}
})
.take(targetFloor + 1 - baseFloor) // ...}
alert
鏉ュ憡璇変綘锛氭垜杩欎釜浜嬩欢娴佹槸鐭ラ亾杩欎簺鐘舵€佺殑锛佷笉杩囩洰鍓嶄粛鐒舵病瑙e喅鍒颁笉浜嗕竴妤肩殑闂鈥︹€?/p>
Final Step
filter
鏉ユ搷浣滀簨浠舵祦锛?/p>
switchMap
鐨勪竴涓簨浠讹紝閭d箞鎴戜滑灏卞彲浠ョ洿鎺ュ湪 switchMap
鍓嶆斁缃竴涓?nbsp;filter
鏉ヨ繃婊ゆ帀鏃犲叧鐨勬寜閽簨浠讹細 return Observable
.fromEvent(emitter, type)
.filter(({ floors, targetFloor, currFloor, currDirection }) => { // 鍙傝€冧笂鏂囬€昏緫鍒ゆ柇
if (currDirection === 'down') return false
else if (currDirection === 'up' && targetFloor <= currFloor) { return false
} else return true
})
Wrap Up
鈫?/code> 鎸夐挳
鈫?/code> 鎸夐挳鍚屾牱鍙槸寮曞叆浜嗘柊鐨勩€愬喅瀹氳銆戠姸鎬佽€屽凡鈥︹€﹁櫧鐒惰繖涔堣鏈変簺涓嶈礋璐d换锛屼笉杩囦粠鎴戜滑宸叉湁鐨勫疄鐜版潵鐪?Rx 浜嬩欢娴佺‘瀹炴槸鍏峰浼橀泤瑙e喅杩欎簺闂鐨勮兘鍔涚殑銆?/p>
涓嶈繃濡傛灉浣犵殑闇€姹備粎浠呮槸銆愭暟鎹姞杞芥椂鏄剧ず Loading 鐘舵€併€戯紝閭d箞寮曞叆 Rx 澶氬皯灏辨湁浜涙潃楦$敤鐗涘垁浜嗐€?/p>