自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

鴻蒙JS UI 組件通信總結(jié)及任意組件通信

系統(tǒng)
組件間的通信主要作用是能在組件間傳遞數(shù)據(jù)或者執(zhí)行相關(guān)的業(yè)務(wù)邏輯,對于鴻蒙應(yīng)用組件,下面將對幾種組件間的通信方式進行代碼實現(xiàn),其中包括實現(xiàn)自定義實現(xiàn)任意組件通信。

[[419195]]

想了解更多內(nèi)容,請訪問:

51CTO和華為官方合作共建的鴻蒙技術(shù)社區(qū)

https://harmonyos.51cto.com

組件間的通信主要作用是能在組件間傳遞數(shù)據(jù)或者執(zhí)行相關(guān)的業(yè)務(wù)邏輯,對于鴻蒙應(yīng)用組件,下面將對幾種組件間的通信方式進行代碼實現(xiàn),其中包括實現(xiàn)自定義實現(xiàn)任意組件通信。

首先我們準備好幾個組件 parent組件、current組件、child1組件、child2組件,其中parent與current是父子組件關(guān)系、curren與child1/child2是父子組件關(guān)系、child1與child2是兄弟組件關(guān)系、parent與child1/child2是跨層級組件的關(guān)系。

common.css

  1. .title { 
  2.     font-size: 20px; 
  3.     text-align: left
  4.     padding-bottom: 5px; 
  5.     line-height: 20px; 
  6. .div-button { 
  7.     flex-direction: row; 
  8. .div-button .button { 
  9.     margin: 5px; 

1.parent組件

parent.hml

  1. <element src="../current/current.hml" name="current" ></element> 
  2. <div class="container"
  3.     <text class="title">parent-num : {{num}}</text> 
  4.     <text class="title">parent-info-str : {{info.str}}</text> 
  5.     <current id="current" title="parent" ></current
  6. </div> 

parent.js

  1. export default { 
  2.     data: { 
  3.         num: 0, 
  4.         info:{ 
  5.             str: 'parentStr' 
  6.         } 
  7.     } 

parent.css

  1. @import '../../../common/css/common.css'
  2. .container { 
  3.     display: flex; 
  4.     flex: 1; 
  5.     flex-direction: column
  6.     width: 100%; 
  7.     padding: 10px; 
  8.     background-color: aqua; 

2.current組件

current.hml

  1. <element src="../child1/child.hml" name="child1" ></element> 
  2. <element src="../child2/child.hml" name="child2" ></element> 
  3. <div class="container"
  4.     <text class="title">current-title : {{title}}</text> 
  5.     <text class="title">current-num : {{num}}</text> 
  6.     <text class="title">current-info-str : {{info.str}}</text> 
  7.     <div class="div-button"
  8.         <button class="button" type="capsule" value="改變parent" onclick="changeParent"></button> 
  9.         <button class="button" type="capsule" value="改變child1" onclick="changeChild1" ></button> 
  10.         <button class="button" type="capsule" value="改變child2" onclick="changeChild2" ></button> 
  11.     </div> 
  12.     <child1 id="child1" ></child1> 
  13.     <child2 id="child2" ></child2> 
  14. </div> 

current.js

  1. export default { 
  2.     props:{ 
  3.         title:{ 
  4.             default'current-title'
  5.         } 
  6.     }, 
  7.     data: { 
  8.         num: 0, 
  9.         info:{ 
  10.             str: 'currentStr' 
  11.         } 
  12.     }, 
  13.   
  14.     // 改變parent組件的num 和 info 
  15.     changeParent(){ 
  16.         console.log('**start**********************'); 
  17.         console.log('current change parent'); 
  18.   
  19.         let parent = this.$parent(); 
  20.         let num = parent.num + 1; 
  21.         parent.num = num; 
  22.         let info = { 
  23.             str: 'current change parent' 
  24.         }; 
  25. //        current.info = info; 
  26.   
  27.         parent.info.str = 'current change parent'
  28.   
  29.         console.log('num :'); 
  30.         console.log(num); 
  31.         console.log('info :'); 
  32.         console.log(JSON.stringify(info)); 
  33.         console.log('**end************************'); 
  34.     }, 
  35.   
  36.     // 改變child1組件的num 和 info 
  37.     changeChild1(){ 
  38.         console.log('**start**********************'); 
  39.         console.log('current change child1'); 
  40.   
  41.         let child1 = this.$child('child1'); 
  42.         let num = child1.num + 1; 
  43.         child1.num = num; 
  44.         let info = { 
  45.             str: 'current change child1' 
  46.         }; 
  47.         child1.info = info; 
  48.   
  49.         console.log('num :'); 
  50.         console.log(num); 
  51.         console.log('info :'); 
  52.         console.log(JSON.stringify(info)); 
  53.         console.log('**end************************'); 
  54.     }, 
  55.   
  56.     // 改變child2組件的num 和 info 
  57.     changeChild2(){ 
  58.         console.log('**start**********************'); 
  59.         console.log('current change child2'); 
  60.         let child2 = this.$child('child2'); 
  61.         let num = child2.num + 1; 
  62.         child2.num = num; 
  63.         let info = { 
  64.             str: 'current change child2' 
  65.         }; 
  66.         child2.info = info; 
  67.   
  68.         console.log('num :'); 
  69.         console.log(num); 
  70.         console.log('info :'); 
  71.         console.log(JSON.stringify(info)); 
  72.         console.log('**end************************'); 
  73.     } 

current.css

  1. @import '../../../common/css/common.css'
  2. .container { 
  3.     display: flex; 
  4.     flex: 1; 
  5.     flex-direction: column
  6.     width: 100%; 
  7.     padding: 10px; 
  8.     background-color: aquamarine; 
  9.     border: 1px solid #333333; 

3.child1組件

child1.hml

  1. <div class="container" style="line-height: 40px;"
  2.     <text class="title">child1-num : {{num}}</text> 
  3.     <text class="title">child1-info-str : {{info.str}}</text> 
  4. </div> 

child1.js

  1. export default { 
  2.     data: { 
  3.         num: 0, 
  4.         info:{ 
  5.             str: 'child1Str' 
  6.         } 
  7.     } 

child1.css

  1. @import '../../../common/css/common.css'
  2. .container { 
  3.     display: flex; 
  4.     flex-direction: column
  5.     width: 100%; 
  6.     padding: 10px; 
  7.     background-color: bisque; 
  8.     border: 1px solid #333333; 
  9.     min-height: 200px; 

4.child2組件

child2.hml

  1. <div class="container"
  2.     <text class="title">child2-num : {{num}}</text> 
  3.     <text class="title">child2-info-str : {{info.str}}</text> 
  4. </div> 

child2.js

  1. export default { 
  2.   
  3.     data: { 
  4.         num: 0, 
  5.         info:{ 
  6.             str: 'child2Str' 
  7.         } 
  8.     } 

child2.css

  1. @import '../../../common/css/common.css'
  2. .container { 
  3.     display: flex; 
  4.     flex-direction: column
  5.     width: 100%; 
  6.     padding: 10px; 
  7.     background-color: beige; 
  8.     border: 1px solid #333333; 
  9.     margin-top: 10px; 
  10.     min-height: 200px; 

最終預(yù)覽得到結(jié)果如下:

鴻蒙JS UI 組件通信總結(jié)及任意組件通信-鴻蒙HarmonyOS技術(shù)社區(qū)

父子關(guān)系通信

parent與current是父子組件關(guān)系,在current.js中props的title=''current-title",在parent.hml中使用current組件時傳入title="parent"時,current組件顯示parent。

鴻蒙JS UI 組件通信總結(jié)及任意組件通信-鴻蒙HarmonyOS技術(shù)社區(qū)
鴻蒙JS UI 組件通信總結(jié)及任意組件通信-鴻蒙HarmonyOS技術(shù)社區(qū)

curren與child1/child2是父子組件關(guān)系,在current組件中有三個按鈕 改變parent、改變child1、改變child2

鴻蒙JS UI 組件通信總結(jié)及任意組件通信-鴻蒙HarmonyOS技術(shù)社區(qū)

在curren組件中,點擊改變parent按鈕會觸發(fā)changeParent方法,點擊改變child1按鈕會觸發(fā)changeChild1方法,點擊改變child1按鈕會觸發(fā)changeChild1方法,這些方法會改變對應(yīng)組件中的數(shù)據(jù),如下:

點擊改變parent

鴻蒙JS UI 組件通信總結(jié)及任意組件通信-鴻蒙HarmonyOS技術(shù)社區(qū)

點擊改變child1

鴻蒙JS UI 組件通信總結(jié)及任意組件通信-鴻蒙HarmonyOS技術(shù)社區(qū)

點擊改變child2

鴻蒙JS UI 組件通信總結(jié)及任意組件通信-鴻蒙HarmonyOS技術(shù)社區(qū)

在changeParent方法中使用this.$parent()方法來獲取了parent組件的ViewModel,從而可以訪問和改變parent組件的數(shù)據(jù)

在changeChild1和changeChild2中使用了this.$child(id)方法來獲取child1/child2組件的ViewModel,從而可以訪問和改變child1/child2組件的數(shù)據(jù)

除了使用this.$parent方法外還可以使用自定義事件的方式來進行子組件調(diào)用父組件方法來向父組件傳值,parent組件在引用current組件時綁定事件,在current組件中調(diào)用this.$emit方法來觸發(fā)綁定的自定義事件。

parent.hml

鴻蒙JS UI 組件通信總結(jié)及任意組件通信-鴻蒙HarmonyOS技術(shù)社區(qū)

parent.js

鴻蒙JS UI 組件通信總結(jié)及任意組件通信-鴻蒙HarmonyOS技術(shù)社區(qū)

current.js

鴻蒙JS UI 組件通信總結(jié)及任意組件通信-鴻蒙HarmonyOS技術(shù)社區(qū)

總結(jié)下來,父子組件間的通信方式大概有幾種方法:

props:用于父組件向子組件傳遞數(shù)據(jù)

$emit:用于自組件向父組件傳遞數(shù)據(jù)或調(diào)用父組件方法

$parent:用于獲取父組件ViewModel從而可以修改父組件數(shù)據(jù)和調(diào)用父組件方法

$child:用于獲取子組件ViewModel從而修改子組件數(shù)據(jù)和調(diào)用子組件方法

兄弟關(guān)系與跨層級組件間通信

根據(jù)上面父子組件的通信方式,對于兄弟組件和跨層級組件之間,可以組合使用上面的幾種方法組合的方式可以實現(xiàn)非父子組件間的通信,但是如果組件間嵌套比較復(fù)雜,嵌套層級比較多的情況下,使用以上方法組合的方式顯然不太方便,在這里嘗試實現(xiàn)一種類似vue bus全局通信的方式來進行任意組件間的通信。

在JS API中app.js文件中定義的數(shù)據(jù)可以通過this.app.app.def獲取,根據(jù)這個特性和使用觀察者模式來設(shè)計一可以全局訂閱響應(yīng)的通信模式。

鴻蒙JS UI 組件通信總結(jié)及任意組件通信-鴻蒙HarmonyOS技術(shù)社區(qū)

首先我們定義evnetBus.js文件,在里面定義相關(guān)的訂閱、發(fā)布和取消訂閱的方法:

eventBus.js

  1. const Bus = { 
  2.     // 發(fā)布的事件集合 
  3.     $events:{}, 
  4.   
  5.     /** 
  6.      * 發(fā)布事件方法 
  7.      * type: string 字符串 
  8.      * fun: function 綁定的方法 
  9.      * */ 
  10.     $on(type,fun){ 
  11.   
  12.     }, 
  13.   
  14.     /** 
  15.      * 觸發(fā)事件方法 
  16.      * type: string 發(fā)布事件的字符串 
  17.      * args: 傳參 
  18.      * */ 
  19.     $emit(type,...args){ 
  20.   
  21.     }, 
  22.   
  23.     /** 
  24.     * 注銷事件方法 
  25.     * type: string 字符串 
  26.     * fun: string|function 發(fā)布事件時返回的值或者發(fā)布的原function 
  27.     * */ 
  28.     $off(type,fun){ 
  29.       
  30.     } 
  31. export default Bus; 

在里面我們定義了$events對象用于綁定事件的存放,$on方法用于綁定事件,$emit方法用于觸發(fā)事件,$off方法用于注銷事件,完整的eventBus.js如下

eventBus.js

  1. const Bus = { 
  2.     // 發(fā)布的事件集合 
  3.     $events:{ }, 
  4.   
  5.     /** 
  6.      * 發(fā)布事件方法 
  7.      * type: string 字符串 
  8.      * fun: function 綁定的方法 
  9.      * */ 
  10.     $on(type,fun){ 
  11.         let res = ""
  12.         if(type && typeof type == "string" && fun && typeof fun =="function"){ 
  13.             let events = this.$events[type]; 
  14.             if(events){ 
  15.                 let index = events.findIndex(null); 
  16.                 if(index > -1){ 
  17.                     res = `${String(index)}${type}`; 
  18.                     events[index] = fun; 
  19.                 }else
  20.                     events.push(fun); 
  21.                 } 
  22.             }else
  23.                 this.$events[type] = [fun]; 
  24.                 res = `0${type}`; 
  25.             } 
  26.   
  27.         } 
  28.         return res; 
  29.     }, 
  30.   
  31.     /** 
  32.      * 觸發(fā)事件方法 
  33.      * type: string 發(fā)布事件的字符串 
  34.      * args: 傳參 
  35.      * */ 
  36.     $emit(type,...args){ 
  37.         if(type && typeof type == "string"){ 
  38.             let events = this.$events[type]; 
  39.             if(events){ 
  40.                 events.forEach(fun => { 
  41.                     if(fun && typeof fun =="function"){ 
  42.                         fun(...args); 
  43.                     } 
  44.                 }); 
  45.             } 
  46.         } 
  47.     }, 
  48.   
  49.     /** 
  50.      * 注銷事件方法 
  51.      * type: string 字符串 
  52.      * fun: string|function 發(fā)布事件時返回的值或者發(fā)布的原function 
  53.      * */ 
  54.     $off(type,fun){ 
  55.         if(type && typeof type == "string" && fun){ 
  56.             let events = this.$events[type]; 
  57.             if(events){ 
  58.                 if(typeof fun == "string"){ 
  59.                     let indexStr = fun.replace(type,''); 
  60.                     let index = parseInt(indexStr); 
  61.                     events[index] = null
  62.                 } 
  63.                 if(typeof fun == "function"){ 
  64.                     events.forEach(item => { 
  65.                         if(item == fun){ 
  66.                             item = null
  67.                         } 
  68.                     }); 
  69.                 } 
  70.             } 
  71.         } 
  72.     } 
  73.   
  74. export default Bus; 

使用方法如下:

首先在app.js中引入eventBus:

鴻蒙JS UI 組件通信總結(jié)及任意組件通信-鴻蒙HarmonyOS技術(shù)社區(qū)

在parent組件的onInit方法中綁定事件,可以通過綁定的事件來改變parent組件的值:

鴻蒙JS UI 組件通信總結(jié)及任意組件通信-鴻蒙HarmonyOS技術(shù)社區(qū)

然后我們修改child1組件,添加按鈕來觸發(fā)bus事件:

鴻蒙JS UI 組件通信總結(jié)及任意組件通信-鴻蒙HarmonyOS技術(shù)社區(qū)

在觸發(fā)方法的時候我們傳入了自定義參數(shù)數(shù)據(jù)"123456789"

鴻蒙JS UI 組件通信總結(jié)及任意組件通信-鴻蒙HarmonyOS技術(shù)社區(qū)

在點擊按鈕后將會觸發(fā)我們在parent組件中綁定的方法從而改變parent組件數(shù)據(jù),同時也接收到了child組件傳過來的數(shù)據(jù)"123456789"

鴻蒙JS UI 組件通信總結(jié)及任意組件通信-鴻蒙HarmonyOS技術(shù)社區(qū)

在此就實現(xiàn)了跨層級組件間的通信,此方法可以對任意組件間的事件觸發(fā)和傳值都有效,限于篇幅不再展示,最后我們還要在組件銷毀的時候注銷我們綁定的事件。

我們定義一個注銷事件的按鈕并綁定注銷事件:

鴻蒙JS UI 組件通信總結(jié)及任意組件通信-鴻蒙HarmonyOS技術(shù)社區(qū)

在我們$on綁定事件的時候可以得到一個返回值,這個值也可以用于事件的注銷,在這里就不做演示了,有興趣的可以下去嘗試一下。

以上就是使用觀察者模式實現(xiàn)了一個簡易版的事件總線機制可以在任意組件間通信,基本功能可以實現(xiàn),還有一些缺點需要提示:

1.首先這個在使用的時候必須依賴鴻蒙JS API this.$app.$def 這個方法,每次使用都要用這個來獲取eventBus對象。

2.在注冊綁定事件后一定要記得在當前組件銷毀的時候注銷綁定的事件,否則可能會造成內(nèi)存泄露。

3.事件注銷有兩種方式,需要稍微理解注銷事件的原理才能熟練使用。

文章相關(guān)附件可以點擊下面的原文鏈接前往下載

TestApp.rar

想了解更多內(nèi)容,請訪問:

51CTO和華為官方合作共建的鴻蒙技術(shù)社區(qū)

https://harmonyos.51cto.com

 

責任編輯:jianghua 來源: 鴻蒙社區(qū)
相關(guān)推薦

2024-01-09 08:34:56

Vue3.js組件通信

2021-09-14 09:34:05

鴻蒙HarmonyOS應(yīng)用

2022-03-11 12:31:04

Vue3組件前端

2023-11-13 09:28:20

跨組件組件化

2021-08-03 12:47:58

鴻蒙HarmonyOS應(yīng)用

2016-09-14 21:44:50

JavascriptreactJsjsx

2021-08-02 08:22:33

BlazorEventCallba通信

2019-05-29 14:23:53

Vue.js組件通信

2022-01-25 16:54:14

BLE操作系統(tǒng)鴻蒙

2023-01-05 07:39:28

2023-09-28 08:00:53

2019-04-10 08:24:06

vue組件通信

2019-08-14 10:00:08

vue組件通信前端

2019-02-26 10:33:24

快應(yīng)用

2023-04-19 15:29:53

通信技巧Vue 3開發(fā)

2022-05-06 08:47:10

Vue 3組件前端

2025-03-04 08:56:31

2022-07-27 08:40:06

父子組件VUE3

2024-12-24 07:38:44

C#串口通信

2019-05-15 08:00:00

vue組件間通信前端
點贊
收藏

51CTO技術(shù)棧公眾號