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

在ArkUI的ETS中實(shí)現(xiàn)【插槽】的功能

開發(fā) 前端
插槽是一套內(nèi)容分發(fā)的API,當(dāng)組件渲染的時(shí)候,將會(huì)被替換為“Your Profile”。插槽內(nèi)可以包含任何模板代碼。

[[436541]]

想了解更多內(nèi)容,請(qǐng)?jiān)L問:

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

https://harmonyos.51cto.com

距離ETS的發(fā)布也有一段時(shí)間,也有不少小伙伴通過ETS制作出很多精美的頁面,但在我查閱ETS的組件和API中發(fā)現(xiàn),現(xiàn)有版本的ETS并沒有插槽的功能。經(jīng)過一段時(shí)間的探索終于找到曲線救國方式實(shí)現(xiàn)插槽功能,得以讓組件之間進(jìn)行解耦。

什么是插槽

了解插槽的小伙伴可以跳過

vue官方定義是:插槽是一套內(nèi)容分發(fā)的API,當(dāng)組件渲染的時(shí)候, 將會(huì)被替換為“Your Profile”。插槽內(nèi)可以包含任何模板代碼。

通俗一點(diǎn)就是插槽就像一個(gè)占位符,將組件外的內(nèi)容通過API分發(fā)至組件內(nèi)。

實(shí)現(xiàn)步驟

定義一個(gè)slot類

旨在提供一個(gè)具名的插槽,故定義一個(gè)slot類做后續(xù)委托。這不是實(shí)現(xiàn)的關(guān)鍵點(diǎn),也可不定義。

  1. class Slot{ 
  2.   name:string="default" 
  3.   builder:any 
  4.  
  5.   constructor (name:string,builder:any){ 
  6.     this.name=name
  7.     this.builder=builder 
  8.   } 

創(chuàng)建一個(gè)組件CompA

創(chuàng)建一個(gè)自定義組件CompA,并提供兩個(gè)具名插槽的處理,一個(gè)defualt,一個(gè)slot2。

  1. @Component 
  2. struct CompA{ 
  3.   @State text:string="" 
  4.   @State data:string[]=[] 
  5.   @State slot:Slot=new Slot(null
  6.   build(){ 
  7.     Column(){ 
  8.       Column(){ 
  9.         Text("CompA組件內(nèi)的內(nèi)容"
  10.         .fontColor("#00F"
  11.         .fontSize(16) 
  12.         .margin(10) 
  13.       } 
  14.       Column(){ 
  15.         Row(){ 
  16.           if(this.slot.name=="default"){ 
  17.             ForEach(["這是默認(rèn)插槽【default】"], 
  18.               this.slot.builder) 
  19.           } 
  20.           if(this.slot.name=="slot2"){ 
  21.             ForEach(this.data, 
  22.               this.slot.builder) 
  23.           } 
  24.         } 
  25.       } 
  26.     } 
  27.   } 

構(gòu)建頁面的組件

構(gòu)建一個(gè)Index的頁面,在頁面內(nèi)創(chuàng)建兩個(gè)Buider bulder1 ,builder2,并實(shí)例化兩個(gè)Slot類slot1、slot2,將builder1,builder2分別給到slot1,slot2。

builder1內(nèi)通過Text組件顯示一段文字。

builder2內(nèi)通構(gòu)建稍微復(fù)雜一點(diǎn)的模型,設(shè)置一個(gè)文字和二維碼。

  1. @Entry 
  2. @Component 
  3. struct Index { 
  4.   @Builder builder1(str:string){ 
  5.     Text(str).fontSize(18).fontColor("#f00"
  6.   } 
  7.  
  8.   @Builder builder2(obj:any){ 
  9.     Column(){ 
  10.       Row(){ 
  11.         Text(obj.title).fontSize(16) 
  12.       } 
  13.       Row(){ 
  14.         QRCode(obj.title).width(100).height(100) 
  15.       }.margin(10) 
  16.     }.margin(10) 
  17.   } 
  18.  
  19.    slot1:Slot=new Slot(this.builder1) 
  20.    slot2:Slot=new Slot(this.builder2,"slot2"
  21.  
  22.   build() { 
  23.     Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 
  24.       Column(){ 
  25.          CompA(){ 
  26.            Text("這樣是不會(huì)顯示的").fontSize(24) 
  27.          } 
  28.         CompA({slot:this.slot1}) 
  29.         CompA({slot:this.slot2,data:[{title:"這是第二個(gè)插槽"},{title:"http://www.baidu.com"}]}) 
  30.       } 
  31.     } 
  32.     .width('100%'
  33.     .height('100%'
  34.   } 

顯示效果:

在ArkUI的ETS中實(shí)現(xiàn)【插槽】的功能-鴻蒙HarmonyOS技術(shù)社區(qū)

通過圖片可以看到,builder1,builder2真實(shí)位置是在了CompA的slot處。

重點(diǎn)

上面就提到Slot類可以不用創(chuàng)建,因?yàn)閷?shí)現(xiàn)原理是通過ForEach+Builder實(shí)現(xiàn),也可以將Builder通過函數(shù)綁定到組件內(nèi)。

再看一下官方文檔中ForEach:

在ArkUI的ETS中實(shí)現(xiàn)【插槽】的功能-鴻蒙HarmonyOS技術(shù)社區(qū)

全部代碼供參考

  1. @Entry 
  2. @Component 
  3. struct Index { 
  4.   @Builder builder1(str:string){ 
  5.     Text(str).fontSize(18).fontColor("#f00"
  6.   } 
  7.   @Builder builder2(obj:any){ 
  8.     Column(){ 
  9.       Row(){ 
  10.         Text(obj.title).fontSize(16) 
  11.       } 
  12.       Row(){ 
  13.         QRCode(obj.title).width(100).height(100) 
  14.       }.margin(10) 
  15.     }.margin(10) 
  16.   } 
  17.  slot1:Slot=new Slot(this.builder1) 
  18.  slot2:Slot=new Slot(this.builder2,"slot2"
  19.   build() { 
  20.     Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 
  21.       Column(){ 
  22.          CompA(){ 
  23.            Text("這樣是不會(huì)顯示的").fontSize(24) 
  24.          } 
  25.         CompA({slot:this.slot1}) 
  26.         CompA({slot:this.slot2,data:[{title:"這是第二個(gè)插槽"},{title:"http://www.baidu.com"}]}) 
  27.       } 
  28.     } 
  29.     .width('100%'
  30.     .height('100%'
  31.   } 
  32.  
  33. @Component 
  34. struct CompA{ 
  35.   @State text:string="" 
  36.   @State data:string[]=[] 
  37.   @State slot:Slot=new Slot(null
  38.   build(){ 
  39.     Column(){ 
  40.       Column(){ 
  41.         Text("CompA組件內(nèi)的內(nèi)容"
  42.         .fontColor("#00F"
  43.         .fontSize(16) 
  44.         .margin(10) 
  45.       } 
  46.       Column(){ 
  47.         Row(){ 
  48.           if(this.slot.name=="default"){ 
  49.             ForEach(["這是默認(rèn)插槽【default】"], 
  50.               this.slot.builder) 
  51.           } 
  52.           if(this.slot.name=="slot2"){ 
  53.             ForEach(this.data, 
  54.               this.slot.builder) 
  55.           } 
  56.         } 
  57.       } 
  58.     } 
  59.   } 
  60. class Slot{ 
  61.   name:string="default" 
  62.   builder:any 
  63.   constructor (builder:any,name?:string){ 
  64.     name && (this.name=name); 
  65.     this.builder=builder 
  66.   } 

想了解更多內(nèi)容,請(qǐng)?jiān)L問:

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

https://harmonyos.51cto.com

 

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

2009-07-09 16:12:53

WeblogicJDBC

2022-08-12 19:13:07

etswifi連接操作

2022-09-05 15:22:27

ArkUIets

2022-02-23 15:07:22

HarmonyOS常用控制ArkUI-eTS

2022-10-24 14:49:54

ArkUI心電圖組件

2022-07-04 16:34:46

流光按鈕Stack

2022-11-02 16:06:54

ArkUIETS

2022-02-23 15:36:46

ArkUI-eTS事件監(jiān)聽鴻蒙

2022-01-25 17:05:44

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

2022-07-05 16:13:37

ArkUI-eTS智能晾曬系統(tǒng)

2020-05-25 17:03:47

Vue嵌套插槽開發(fā)

2023-03-13 15:03:05

鴻蒙ArkUI

2022-09-01 21:56:34

KubernetesLinkerd

2023-10-27 16:15:35

鴻蒙天氣服務(wù)功能

2024-01-02 11:15:46

Linux系統(tǒng)

2024-06-07 11:48:32

2022-10-09 15:13:18

TextPickerArkUI eTS

2022-10-10 14:51:51

ArkUI eTSPieChart組件

2023-04-25 08:01:23

JavaQuarkusKubernetes

2010-01-10 17:47:51

點(diǎn)贊
收藏

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