1# 自定义组件成员属性访问限定符使用限制 2 3 4ArkTS会对自定义组件的成员变量使用的访问限定符private/public/protected进行校验,当不按规范使用访问限定符private/public/protected时,会产生对应的日志信息。 5 6 7> **说明:** 8> 9> 从API version 12开始,支持自定义组件成员属性访问限定符使用限制的规则。 10 11 12## 使用限制 13 14- 对于\@State/\@Prop/\@Provide/\@BuilderParam/常规成员变量(不涉及更新的普通变量),当使用private修饰时,在自定义组件构造时,不允许进行赋值传参,否则会有编译告警日志提示。 15 16- 对于\@StorageLink/\@StorageProp/\@LocalStorageLink/\@LocalStorageProp/\@Consume变量,当使用public修饰时,会有编译告警日志提示。 17 18- 对于\@Link/\@ObjectLink变量,当使用private修饰时,会有编译告警日志提示。 19 20- 由于struct没有继承能力,上述所有的这些变量使用protected修饰时,会有编译告警日志提示。 21 22- 当\@Require和private同时修饰自定义组件struct的\@State/\@Prop/\@Provide/\@BuilderParam/常规成员变量(不涉及更新的普通变量)时,会有编译告警日志提示。 23 24 25## 错误使用场景示例 26 271.当成员变量被private访问限定符和\@State/\@Prop/\@Provide/\@BuilderParam装饰器同时修饰,并且通过父组件进行初始化赋值,ArkTS会进行校验并产生告警日志。 28 29```ts 30@Entry 31@Component 32struct AccessRestrictions { 33 @Builder 34 buildTest() { 35 Text("Parent builder") 36 } 37 38 build() { 39 Column() { 40 ComponentsChild({ 41 state_value: "Hello", 42 prop_value: "Hello", 43 provide_value: "Hello", 44 builder_value: this.buildTest, 45 regular_value: "Hello" 46 }) 47 } 48 .width('100%') 49 } 50} 51 52@Component 53struct ComponentsChild { 54 @State private state_value: string = "Hello"; 55 @Prop private prop_value: string = "Hello"; 56 @Provide private provide_value: string = "Hello"; 57 @BuilderParam private builder_value: () => void = this.buildTest; 58 private regular_value: string = "Hello"; 59 60 @Builder 61 buildTest() { 62 Text("Child builder") 63 } 64 65 build() { 66 Column() { 67 Text("Hello") 68 .fontSize(50) 69 .fontWeight(FontWeight.Bold) 70 } 71 } 72} 73``` 74 75编译告警日志如下: 76 77```ts 78Property 'state_value' is private and can not be initialized through the component constructor. 79Property 'prop_value' is private and can not be initialized through the component constructor. 80Property 'provide_value' is private and can not be initialized through the component constructor. 81Property 'builder_value' is private and can not be initialized through the component constructor. 82Property 'regular_value' is private and can not be initialized through the component constructor. 83``` 84 852.当成员变量被public访问限定符和\@StorageLink/\@StorageProp/\@LocalStorageLink/\@LocalStorageProp/\@Consume装饰器同时修饰,并且通过父组件进行初始化赋值,ArkTS会进行校验并产生告警日志。 86 87```ts 88@Entry 89@Component 90struct AccessRestrictions { 91 @Provide consume_value: string = "Hello"; 92 build() { 93 Column() { 94 ComponentChild() 95 } 96 .width('100%') 97 } 98} 99 100@Component 101struct ComponentChild { 102 @LocalStorageProp("sessionLocalProp") public local_prop_value: string = "Hello"; 103 @LocalStorageLink("sessionLocalLink") public local_link_value: string = "Hello"; 104 @StorageProp("sessionProp") public storage_prop_value: string = "Hello"; 105 @StorageLink("sessionLink") public storage_link_value: string = "Hello"; 106 @Consume public consume_value: string; 107 build() { 108 Column() { 109 Text("Hello") 110 .fontSize(50) 111 .fontWeight(FontWeight.Bold) 112 } 113 } 114} 115``` 116 117编译告警日志如下: 118 119```ts 120Property 'local_prop_value' can not be decorated with both @LocalStorageProp and public. 121Property 'local_link_value' can not be decorated with both @LocalStorageLink and public. 122Property 'storage_prop_value' can not be decorated with both @StorageProp and public. 123Property 'storage_link_value' can not be decorated with both @StorageLink and public. 124Property 'consume_value' can not be decorated with both @Consume and public. 125``` 126 1273.当成员变量被private访问限定符和\@Link/\@ObjectLink装饰器同时修饰,并且通过父组件进行初始化赋值,ArkTS会进行校验并产生告警日志。 128 129```ts 130@Entry 131@Component 132struct AccessRestrictions { 133 @State link_value: string = "Hello"; 134 @State objectLink_value: ComponentObj = new ComponentObj(); 135 build() { 136 Column() { 137 ComponentChild({link_value: this.link_value, objectLink_value: this.objectLink_value}) 138 } 139 .width('100%') 140 } 141} 142 143@Observed 144class ComponentObj { 145 count: number = 0; 146} 147@Component 148struct ComponentChild { 149 @Link private link_value: string; 150 @ObjectLink private objectLink_value: ComponentObj; 151 build() { 152 Column() { 153 Text("Hello") 154 .fontSize(50) 155 .fontWeight(FontWeight.Bold) 156 } 157 } 158} 159``` 160 161编译告警日志如下: 162 163```ts 164Property 'link_value' can not be decorated with both @Link and private. 165Property 'objectLink_value' can not be decorated with both @ObjectLink and private. 166``` 167 1684.当成员变量被protected访问限定符修饰,并且通过父组件进行初始化赋值,ArkTS会进行校验并产生告警日志。 169 170```ts 171@Entry 172@Component 173struct AccessRestrictions { 174 build() { 175 Column() { 176 ComponentChild({regular_value: "Hello"}) 177 } 178 .width('100%') 179 } 180} 181 182@Component 183struct ComponentChild { 184 protected regular_value: string = "Hello"; 185 build() { 186 Column() { 187 Text("Hello") 188 .fontSize(50) 189 .fontWeight(FontWeight.Bold) 190 } 191 } 192} 193``` 194 195编译告警日志如下: 196 197```ts 198The member attributes of a struct can not be protected. 199``` 200 2015.当成员变量被private访问限定符、\@Require和\@State/\@Prop/\@Provide/\@BuilderParam装饰器同时修饰,并且通过父组件进行初始化赋值,ArkTS会进行校验并产生告警日志。 202 203```ts 204@Entry 205@Component 206struct AccessRestrictions { 207 build() { 208 Column() { 209 ComponentChild({prop_value: "Hello"}) 210 } 211 .width('100%') 212 } 213} 214@Component 215struct ComponentChild { 216 @Require @Prop private prop_value: string = "Hello"; 217 build() { 218 Column() { 219 Text("Hello") 220 .fontSize(50) 221 .fontWeight(FontWeight.Bold) 222 } 223 } 224} 225``` 226 227编译告警日志如下: 228 229```ts 230Property 'prop_value' can not be decorated with both @Require and private. 231Property 'prop_value' is private and can not be initialized through the component constructor. 232``` 233 234