W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗(yàn)值獎勵
自定義組件的生命周期回調(diào)函數(shù)用于通知用戶該自定義組件的生命周期,這些回調(diào)函數(shù)是私有的,在運(yùn)行時由開發(fā)框架在特定的時間進(jìn)行調(diào)用,不能從應(yīng)用程序中手動調(diào)用這些回調(diào)函數(shù)。
允許在生命周期函數(shù)中使用Promise和異步回調(diào)函數(shù),比如網(wǎng)絡(luò)資源獲取,定時器設(shè)置等;
aboutToAppear?(): void
aboutToAppear函數(shù)在創(chuàng)建自定義組件的新實(shí)例后,在執(zhí)行其build()函數(shù)之前執(zhí)行。允許在aboutToAppear函數(shù)中改變狀態(tài)變量,更改將在后續(xù)執(zhí)行build()函數(shù)中生效。
從API version 9開始,該接口支持在ArkTS卡片中使用。
aboutToDisappear?(): void
aboutToDisappear函數(shù)在自定義組件析構(gòu)銷毀之前執(zhí)行。不允許在aboutToDisappear函數(shù)中改變狀態(tài)變量,特別是@Link變量的修改可能會導(dǎo)致應(yīng)用程序行為不穩(wěn)定。
從API version 9開始,該接口支持在ArkTS卡片中使用。
onBackPress?(): void
當(dāng)用戶點(diǎn)擊返回按鈕時觸發(fā),僅@Entry裝飾的自定義組件生效。
- // xxx.ets
- @Entry
- @Component
- struct IndexComponent {
- @State textColor: Color = Color.Black;
- onPageShow() {
- this.textColor = Color.Blue;
- console.info('IndexComponent onPageShow');
- }
- onPageHide() {
- this.textColor = Color.Transparent;
- console.info('IndexComponent onPageHide');
- }
- onBackPress() {
- this.textColor = Color.Red;
- console.info('IndexComponent onBackPress');
- }
- build() {
- Column() {
- Text('Hello World')
- .fontColor(this.textColor)
- .fontSize(30)
- .margin(30)
- }.width('100%')
- }
- }
onLayout?(children: Array<LayoutChild>, constraint: ConstraintSizeOptions): void
框架會在自定義組件布局時,將該自定義組件的子節(jié)點(diǎn)信息和自身的尺寸范圍通過onLayout傳遞給該自定義組件。不允許在onLayout函數(shù)中改變狀態(tài)變量。
從API version 9開始,該接口支持在ArkTS卡片中使用。
參數(shù):
參數(shù)名 | 類型 | 說明 |
---|---|---|
children | Array<LayoutChild> | 子組件布局信息。 |
constraint | 父組件constraint信息。 |
onMeasure?(children: Array<LayoutChild>, constraint: ConstraintSizeOptions): void
框架會在自定義組件確定尺寸時,將該自定義組件的子節(jié)點(diǎn)信息和自身的尺寸范圍通過onMeasure傳遞給該自定義組件。不允許在onMeasure函數(shù)中改變狀態(tài)變量。
從API version 9開始,該接口支持在ArkTS卡片中使用。
參數(shù):
參數(shù)名 | 類型 | 說明 |
---|---|---|
children | Array<LayoutChild> | 子組件布局信息。 |
constraint | 父組件constraint信息。 |
子組件布局信息。
從API version 9開始,該接口支持在ArkTS卡片中使用。
參數(shù) | 參數(shù)類型 | 描述 |
---|---|---|
name | string | 子組件名稱。 |
id | string | 子組件id。 |
constraint | 子組件約束尺寸。 | |
borderInfo | 子組件border信息。 | |
position | 子組件位置坐標(biāo)。 | |
measure | (childConstraint: ConstraintSizeOptions) => void | 調(diào)用此方法對子組件的尺寸范圍進(jìn)行限制。 |
layout | (LayoutInfo: LayoutInfo) => void | 調(diào)用此方法對子組件的位置信息進(jìn)行限制。 |
子組件border信息。
從API version 9開始,該接口支持在ArkTS卡片中使用。
參數(shù) | 參數(shù)類型 | 描述 |
---|---|---|
borderWidth | 邊框?qū)挾阮愋停糜诿枋鼋M件邊框不同方向的寬度。 | |
margin | 外邊距類型,用于描述組件不同方向的外邊距。 | |
padding | 內(nèi)邊距類型,用于描述組件不同方向的內(nèi)邊距。 |
子組件layout信息。
從API version 9開始,該接口支持在ArkTS卡片中使用。
參數(shù) | 參數(shù)類型 | 描述 |
---|---|---|
position | 子組件位置坐標(biāo)。 | |
constraint | 子組件約束尺寸。 |
- // xxx.ets
- @Entry
- @Component
- struct Index {
- build() {
- Column() {
- CustomLayout() {
- ForEach([1, 2, 3], (index) => {
- Text('Sub' + index)
- .fontSize(30)
- .borderWidth(2)
- })
- }
- }
- }
- }
- @Component
- struct CustomLayout {
- @BuilderParam builder: () => {};
- onLayout(children: Array<LayoutChild>, constraint: ConstraintSizeOptions) {
- let pos = 0;
- children.forEach((child) => {
- child.layout({ position: { x: pos, y: pos }, constraint: constraint })
- pos += 100;
- })
- }
- onMeasure(children: Array<LayoutChild>, constraint: ConstraintSizeOptions) {
- let size = 100;
- children.forEach((child) => {
- child.measure({ minHeight: size, minWidth: size, maxWidth: size, maxHeight: size })
- size += 50;
- })
- }
- build() {
- this.builder()
- }
- }
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: