ArkTS會(huì)對(duì)自定義組件的成員變量使用的訪問(wèn)限定符private/public/protected進(jìn)行校驗(yàn),當(dāng)不按規(guī)范使用訪問(wèn)限定符private/public/protected時(shí),會(huì)產(chǎn)生對(duì)應(yīng)的日志信息。
說(shuō)明:
從API version 12開始,支持自定義組件成員屬性訪問(wèn)限定符使用限制的規(guī)則。
使用限制
對(duì)于@State/@Prop/@Provide/@BuilderParam/常規(guī)成員變量(不涉及更新的普通變量),當(dāng)使用private修飾時(shí),在自定義組件構(gòu)造時(shí),不允許進(jìn)行賦值傳參,否則會(huì)有編譯告警日志提示。
對(duì)于@StorageLink/@StorageProp/@LocalStorageLink/@LocalStorageLink/@Consume變量,當(dāng)使用public修飾時(shí),會(huì)有編譯告警日志提示。
對(duì)于@Link/@ObjectLink變量,當(dāng)使用private修飾時(shí),會(huì)有編譯告警日志提示。
由于struct沒(méi)有繼承能力,上述所有的這些變量使用protected修飾時(shí),會(huì)有編譯告警日志提示。
當(dāng)@Require和private同時(shí)修飾自定義組件struct的@State/@Prop/@Provide/@BuilderParam/常規(guī)成員變量(不涉及更新的普通變量)時(shí),會(huì)有編譯告警日志提示。
錯(cuò)誤使用場(chǎng)景示例
1.當(dāng)成員變量被private訪問(wèn)限定符和@State/@Prop/@Provide/@BuilderParam裝飾器同時(shí)修飾時(shí),ArkTS會(huì)進(jìn)行校驗(yàn)并產(chǎn)生告警日志。
@Entry
@Component
struct AccessRestrictions {
@Builder buildTest() {
Text("Parent builder")
}
build() {
Column() {
ComponentsChild({state_value: "Hello", prop_value: "Hello", provide_value: "Hello", builder_value: this.buildTest, regular_value: "Hello"})
}
.width('100%')
}
}
@Component
struct ComponentsChild {
@State private state_value: string = "Hello";
@Prop private prop_value: string = "Hello";
@Provide private provide_value: string = "Hello";
@BuilderParam private builder_value: () => void = this.buildTest;
private regular_value: string = "Hello";
@Builder buildTest() {
Text("Child builder")
}
build() {
Column() {
Text("Hello")
.fontSize(50)
.fontWeight(FontWeight.Bold)
}
}
}
編譯告警日志如下:
Property 'state_value' is private and can not be initialized through the component constructor.
Property 'prop_value' is private and can not be initialized through the component constructor.
Property 'provide_value' is private and can not be initialized through the component constructor.
Property 'builder_value' is private and can not be initialized through the component constructor.
Property 'regular_value' is private and can not be initialized through the component constructor.
2.當(dāng)成員變量被public訪問(wèn)限定符和@StorageLink/@StorageProp/@LocalStorageLink/@LocalStorageLink/@Consume裝飾器同時(shí)修飾時(shí),ArkTS會(huì)進(jìn)行校驗(yàn)并產(chǎn)生告警日志。
@Entry
@Component
struct AccessRestrictions {
@Provide consume_value: string = "Hello";
build() {
Column() {
ComponentChild()
}
.width('100%')
}
}
@Component
struct ComponentChild {
@LocalStorageProp("sessionLocalProp") public local_prop_value: string = "Hello";
@LocalStorageLink("sessionLocalLink") public local_link_value: string = "Hello";
@StorageProp("sessionProp") public storage_prop_value: string = "Hello";
@StorageLink("sessionLink") public storage_link_value: string = "Hello";
@Consume public consume_value: string;
build() {
Column() {
Text("Hello")
.fontSize(50)
.fontWeight(FontWeight.Bold)
}
}
}
編譯告警日志如下:
Property 'local_prop_value' can not be decorated with both @LocalStorageProp and public.
Property 'local_link_value' can not be decorated with both @LocalStorageLink and public.
Property 'storage_prop_value' can not be decorated with both @StorageProp and public.
Property 'storage_link_value' can not be decorated with both @StorageLink and public.
Property 'consume_value' can not be decorated with both @Consume and public.
3.當(dāng)成員變量被private訪問(wèn)限定符和@Link/@ObjectLink裝飾器同時(shí)修飾時(shí),ArkTS會(huì)進(jìn)行校驗(yàn)并產(chǎn)生告警日志。
@Entry
@Component
struct AccessRestrictions {
@State link_value: string = "Hello";
@State objectLink_value: ComponentObj = new ComponentObj();
build() {
Column() {
ComponentChild({link_value: this.link_value, objectLink_value: this.objectLink_value})
}
.width('100%')
}
}
@Observed
class ComponentObj {
count: number = 0;
}
@Component
struct ComponentChild {
@Link private link_value: string;
@ObjectLink private objectLink_value: ComponentObj;
build() {
Column() {
Text("Hello")
.fontSize(50)
.fontWeight(FontWeight.Bold)
}
}
}
編譯告警日志如下:
Property 'link_value' can not be decorated with both @Link and private.
Property 'objectLink_value' can not be decorated with both @ObjectLink and private.
4.當(dāng)成員變量被protected訪問(wèn)限定符修飾時(shí),ArkTS會(huì)進(jìn)行校驗(yàn)并產(chǎn)生告警日志。
@Entry
@Component
struct AccessRestrictions {
build() {
Column() {
ComponentChild({regular_value: "Hello"})
}
.width('100%')
}
}
@Component
struct ComponentChild {
protected regular_value: string = "Hello";
build() {
Column() {
Text("Hello")
.fontSize(50)
.fontWeight(FontWeight.Bold)
}
}
}
編譯告警日志如下:
The member attributes of a struct can not be protected.
5.當(dāng)成員變量被private訪問(wèn)限定符、@Require和@State/@Prop/@Provide/@BuilderParam裝飾器同時(shí)修飾時(shí),ArkTS會(huì)進(jìn)行校驗(yàn)并產(chǎn)生告警日志。
@Entry
@Component
struct AccessRestrictions {
build() {
Column() {
ComponentChild({prop_value: "Hello"})
}
.width('100%')
}
}
@Component
struct ComponentChild {
@Require @Prop private prop_value: string = "Hello";
build() {
Column() {
Text("Hello")
.fontSize(50)
.fontWeight(FontWeight.Bold)
}
}
}
編譯告警日志如下:
Property 'prop_value' can not be decorated with both @Require and private.
Property 'prop_value' is private and can not be initialized through the component constructor.
寫在最后
- 如果你覺(jué)得這篇內(nèi)容對(duì)你還蠻有幫助,我想邀請(qǐng)你幫我三個(gè)小忙:
- 點(diǎn)贊,轉(zhuǎn)發(fā),有你們的 『點(diǎn)贊和評(píng)論』,才是我創(chuàng)造的動(dòng)力。
- 關(guān)注小編,同時(shí)可以期待后續(xù)文章ing??,不定期分享原創(chuàng)知識(shí)。
- 想要獲取更多完整鴻蒙最新學(xué)習(xí)知識(shí)點(diǎn),請(qǐng)移步前往小編:
https://gitee.com/MNxiaona/733GH/blob/master/jianshu
