• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Constraints on Access Modifiers of Custom Component Member Variables
2
3
4In ArkTS, use of the access modifiers – **private**, **public**, and **protected** – for custom component member variables must comply with the constraints described in this topic. Build errors will be reported for any incompliance.
5
6Before reading this topic, you are advised to read [State Management Overview](./arkts-state-management-overview.md).
7
8> **NOTE**
9>
10> The constraints on access modifiers of custom component member variables are supported since API version 12.
11
12
13## Constraints
14
15- For regular variables (which do not involve re-rendering) and variables decorated by [\@State](./arkts-state.md), [\@Prop](./arkts-prop.md), [\@Provide](./arkts-provide-and-consume.md), or [\@BuilderParam](./arkts-builderparam.md), when declared as **private**, value assignment is not allowed during custom component construction.
16
17- For variables decorated by [\@StorageLink](./arkts-appstorage.md), [\@StorageProp](./arkts-appstorage.md), [\@LocalStorageLink](./arkts-localstorage.md), [\@LocalStorageProp](./arkts-localstorage.md), or [\@Consume](./arkts-provide-and-consume.md), **public** access is not allowed.
18
19- For variables decorated by [\@Link](./arkts-link.md) or [\@ObjectLink](./arkts-observed-and-objectlink.md), private access is not allowed.
20
21- Because structs do not support inheritance, none of the preceding variables can be declared as **protected**.
22
23- The regular variables (which do not involve re-rendering) and variables decorated by [\@State](./arkts-state.md), [\@Prop](./arkts-prop.md), [\@Provide](./arkts-provide-and-consume.md), or [\@BuilderParam](./arkts-builderparam.md) in custom components cannot be decorated by both [\@Require](./arkts-require.md) and **private**.
24
25
26## Use Scenarios
27
281. If a member variable is decorated by both the **private** access modifier and the \@State, \@Prop, \@Provide, or \@BuilderParam decorator, and is initialized through the parent component, a build error is reported.
29
30[Negative Example]
31```ts
32@Entry
33@Component
34struct AccessRestrictions {
35  @Builder
36  buildTest() {
37    Text("Parent builder")
38  }
39
40  build() {
41    Column() {
42      ComponentsChild({
43        state_value: "Hello",
44        prop_value: "Hello",
45        provide_value: "Hello",
46        builder_value: this.buildTest,
47        regular_value: "Hello"
48      })
49    }
50    .width('100%')
51  }
52}
53
54@Component
55struct ComponentsChild {
56  // The private access is not allowed and an alarm is reported.
57  @State private state_value: string = "Hello";
58  // The private access is not allowed and an alarm is reported.
59  @Prop private prop_value: string = "Hello";
60  // The private access is not allowed and an alarm is reported.
61  @Provide private provide_value: string = "Hello";
62  // The private access is not allowed and an alarm is reported.
63  @BuilderParam private builder_value: () => void = this.buildTest;
64  // The private access is not allowed and an alarm is reported.
65  private regular_value: string = "Hello";
66
67  @Builder
68  buildTest() {
69    Text("Child builder")
70  }
71
72  build() {
73    Column() {
74      Text("Hello")
75        .fontSize(50)
76        .fontWeight(FontWeight.Bold)
77    }
78  }
79}
80```
81
82The following are some build error examples:
83
84```ts
85Property 'state_value' is private and can not be initialized through the component constructor.
86Property 'prop_value' is private and can not be initialized through the component constructor.
87Property 'provide_value' is private and can not be initialized through the component constructor.
88Property 'builder_value' is private and can not be initialized through the component constructor.
89Property 'regular_value' is private and can not be initialized through the component constructor.
90```
91
92[Positive Example]
93```ts
94@Entry
95@Component
96struct AccessRestrictions {
97  @Builder
98  buildTest() {
99    Text("Parent builder")
100  }
101
102  build() {
103    Column() {
104      ComponentsChild({
105        state_value: "Hello",
106        prop_value: "Hello",
107        provide_value: "Hello",
108        builder_value: this.buildTest,
109        regular_value: "Hello"
110      })
111    }
112    .width('100%')
113  }
114}
115
116@Component
117struct ComponentsChild {
118  @State state_value: string = "Hello";
119  @Prop prop_value: string = "Hello";
120  @Provide provide_value: string = "Hello";
121  @BuilderParam builder_value: () => void = this.buildTest;
122  regular_value: string = "Hello";
123
124  @Builder
125  buildTest() {
126    Text("Child builder")
127  }
128
129  build() {
130    Column() {
131      Text("Hello")
132        .fontSize(50)
133        .fontWeight(FontWeight.Bold)
134    }
135  }
136}
137```
138
1392. If a member variable is decorated by both the **public** access modifier and the \@StorageLink, \@StorageProp, \@LocalStorageLink, \@LocalStorageProp, or \@Consume decorator, and is initialized through the parent component, a build error is reported.
140
141[Negative Example]
142```ts
143@Entry
144@Component
145struct AccessRestrictions {
146  @Provide consume_value: string = "Hello";
147  build() {
148    Column() {
149      ComponentChild()
150    }
151    .width('100%')
152  }
153}
154
155@Component
156struct ComponentChild {
157  // The public access is not allowed and an alarm is reported.
158  @LocalStorageProp("sessionLocalProp") public local_prop_value: string = "Hello";
159  // The public access is not allowed and an alarm is reported.
160  @LocalStorageLink("sessionLocalLink") public local_link_value: string = "Hello";
161  // The public access is not allowed and an alarm is reported.
162  @StorageProp("sessionProp") public storage_prop_value: string = "Hello";
163  // The public access is not allowed and an alarm is reported.
164  @StorageLink("sessionLink") public storage_link_value: string = "Hello";
165  // The public access is not allowed and an alarm is reported.
166  @Consume public consume_value: string;
167
168  build() {
169    Column() {
170      Text("Hello")
171        .fontSize(50)
172        .fontWeight(FontWeight.Bold)
173    }
174  }
175}
176```
177
178The following are some build error examples:
179
180```ts
181Property 'local_prop_value' can not be decorated with both @LocalStorageProp and public.
182Property 'local_link_value' can not be decorated with both @LocalStorageLink and public.
183Property 'storage_prop_value' can not be decorated with both @StorageProp and public.
184Property 'storage_link_value' can not be decorated with both @StorageLink and public.
185Property 'consume_value' can not be decorated with both @Consume and public.
186```
187
188[Positive Example]
189```ts
190@Entry
191@Component
192struct AccessRestrictions {
193  @Provide consume_value: string = "Hello";
194  build() {
195    Column() {
196      ComponentChild()
197    }
198    .width('100%')
199  }
200}
201
202@Component
203struct ComponentChild {
204  @LocalStorageProp("sessionLocalProp") local_prop_value: string = "Hello";
205  @LocalStorageLink("sessionLocalLink") local_link_value: string = "Hello";
206  @StorageProp("sessionProp") storage_prop_value: string = "Hello";
207  @StorageLink("sessionLink") storage_link_value: string = "Hello";
208  @Consume consume_value: string;
209  build() {
210    Column() {
211      Text("Hello")
212        .fontSize(50)
213        .fontWeight(FontWeight.Bold)
214    }
215  }
216}
217```
218
2193. If a member variable is decorated by both the **private** access modifier and the \@Link/ or \@ObjectLink decorator, and is initialized through the parent component, a build error is reported.
220
221[Negative Example]
222```ts
223@Entry
224@Component
225struct AccessRestrictions {
226  @State link_value: string = "Hello";
227  @State objectLink_value: ComponentObj = new ComponentObj();
228  build() {
229    Column() {
230      ComponentChild({link_value: this.link_value, objectLink_value: this.objectLink_value})
231    }
232    .width('100%')
233  }
234}
235
236@Observed
237class ComponentObj {
238  count: number = 0;
239}
240@Component
241struct ComponentChild {
242  // The private access is not allowed and an alarm is reported.
243  @Link private link_value: string;
244  // The private access is not allowed and an alarm is reported.
245  @ObjectLink private objectLink_value: ComponentObj;
246  build() {
247    Column() {
248      Text("Hello")
249        .fontSize(50)
250        .fontWeight(FontWeight.Bold)
251    }
252  }
253}
254```
255
256The following are some build error examples:
257
258```ts
259Property 'link_value' can not be decorated with both @Link and private.
260Property 'objectLink_value' can not be decorated with both @ObjectLink and private.
261```
262
263[Positive Example]
264```ts
265@Entry
266@Component
267struct AccessRestrictions {
268  @State link_value: string = "Hello";
269  @State objectLink_value: ComponentObj = new ComponentObj();
270  build() {
271    Column() {
272      ComponentChild({link_value: this.link_value, objectLink_value: this.objectLink_value})
273    }
274    .width('100%')
275  }
276}
277
278@Observed
279class ComponentObj {
280  count: number = 0;
281}
282@Component
283struct ComponentChild {
284  @Link link_value: string;
285  @ObjectLink objectLink_value: ComponentObj;
286  build() {
287    Column() {
288      Text("Hello")
289        .fontSize(50)
290        .fontWeight(FontWeight.Bold)
291    }
292  }
293}
294```
295
2964. If a member variable is decorated by the **protected** access modifier and is initialized through the parent component, a build error is reported.
297
298[Negative Example]
299```ts
300@Entry
301@Component
302struct AccessRestrictions {
303  build() {
304    Column() {
305      ComponentChild({regular_value: "Hello"})
306    }
307    .width('100%')
308  }
309}
310
311@Component
312struct ComponentChild {
313  // The protected access is not allowed and an alarm is reported.
314  protected regular_value: string = "Hello";
315  build() {
316    Column() {
317      Text("Hello")
318        .fontSize(50)
319        .fontWeight(FontWeight.Bold)
320    }
321  }
322}
323```
324
325The following are some build error examples:
326
327```ts
328The member attributes of a struct can not be protected.
329```
330
331[Positive Example]
332```ts
333@Entry
334@Component
335struct AccessRestrictions {
336  build() {
337    Column() {
338      ComponentChild({regular_value: "Hello"})
339    }
340    .width('100%')
341  }
342}
343
344@Component
345struct ComponentChild {
346  regular_value: string = "Hello";
347  build() {
348    Column() {
349      Text("Hello")
350        .fontSize(50)
351        .fontWeight(FontWeight.Bold)
352    }
353  }
354}
355```
356
3575. If a member variable is decorated by both the **private** access modifier and the \@Require, \@State, \@Prop, \@Provide, or \@BuilderParam decorator, and is initialized through the parent component, a build error is reported.
358
359[Negative Example]
360```ts
361@Entry
362@Component
363struct AccessRestrictions {
364  build() {
365    Column() {
366      ComponentChild({prop_value: "Hello"})
367    }
368    .width('100%')
369  }
370}
371@Component
372struct ComponentChild {
373  // The private access is not allowed and an alarm is reported.
374  @Require @Prop private prop_value: string = "Hello";
375  build() {
376    Column() {
377      Text("Hello")
378        .fontSize(50)
379        .fontWeight(FontWeight.Bold)
380    }
381  }
382}
383```
384
385The following are some build error examples:
386
387```ts
388Property 'prop_value' can not be decorated with both @Require and private.
389Property 'prop_value' is private and can not be initialized through the component constructor.
390```
391
392[Positive Example]
393```ts
394@Entry
395@Component
396struct AccessRestrictions {
397  build() {
398    Column() {
399      ComponentChild({prop_value: "Hello"})
400    }
401    .width('100%')
402  }
403}
404@Component
405struct ComponentChild {
406  @Require @Prop prop_value: string = "Hello";
407  build() {
408    Column() {
409      Text("Hello")
410        .fontSize(50)
411        .fontWeight(FontWeight.Bold)
412    }
413  }
414}
415```
416