1e41f4b71Sopenharmony_ci# \@Require装饰器:校验构造传参
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ci
4e41f4b71Sopenharmony_ci\@Require是校验\@Prop、\@State、\@Provide、\@BuilderParam和普通变量(无状态装饰器修饰的变量)是否需要构造传参的一个装饰器。
5e41f4b71Sopenharmony_ci
6e41f4b71Sopenharmony_ci
7e41f4b71Sopenharmony_ci> **说明:**
8e41f4b71Sopenharmony_ci>
9e41f4b71Sopenharmony_ci> 从API version 11开始对\@Prop/\@BuilderParam进行校验。
10e41f4b71Sopenharmony_ci>
11e41f4b71Sopenharmony_ci> 从API version 12开始对\@State/\@Provide/普通变量(无状态装饰器修饰的变量)进行校验。
12e41f4b71Sopenharmony_ci> 从API version 11开始,该装饰器支持在原子化服务中使用。
13e41f4b71Sopenharmony_ci
14e41f4b71Sopenharmony_ci
15e41f4b71Sopenharmony_ci## 概述
16e41f4b71Sopenharmony_ci
17e41f4b71Sopenharmony_ci当\@Require装饰器和\@Prop、\@State、\@Provide、\@BuilderParam、普通变量(无状态装饰器修饰的变量)结合使用时,在构造该自定义组件时,\@Prop、\@State、\@Provide、\@BuilderParam和普通变量(无状态装饰器修饰的变量)必须在构造时传参。
18e41f4b71Sopenharmony_ci
19e41f4b71Sopenharmony_ci## 限制条件
20e41f4b71Sopenharmony_ci
21e41f4b71Sopenharmony_ci\@Require装饰器仅用于装饰struct内的\@Prop、\@State、\@Provide、\@BuilderParam和普通变量(无状态装饰器修饰的变量)。
22e41f4b71Sopenharmony_ci
23e41f4b71Sopenharmony_ci预览器限制场景请参考[PreviewChecker检测规则](https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/ide-previewer-previewchecker-V5)。
24e41f4b71Sopenharmony_ci
25e41f4b71Sopenharmony_ci## 使用场景
26e41f4b71Sopenharmony_ci
27e41f4b71Sopenharmony_ci当Child组件内使用\@Require装饰器和\@Prop、\@State、\@Provide、\@BuilderParam和普通变量(无状态装饰器修饰的变量)结合使用时,父组件Index在构造Child时必须传参,否则编译不通过。
28e41f4b71Sopenharmony_ci
29e41f4b71Sopenharmony_ci```ts
30e41f4b71Sopenharmony_ci@Entry
31e41f4b71Sopenharmony_ci@Component
32e41f4b71Sopenharmony_cistruct Index {
33e41f4b71Sopenharmony_ci  @State message: string = 'Hello World';
34e41f4b71Sopenharmony_ci
35e41f4b71Sopenharmony_ci  @Builder buildTest() {
36e41f4b71Sopenharmony_ci    Row() {
37e41f4b71Sopenharmony_ci      Text('Hello, world')
38e41f4b71Sopenharmony_ci        .fontSize(30)
39e41f4b71Sopenharmony_ci    }
40e41f4b71Sopenharmony_ci  }
41e41f4b71Sopenharmony_ci
42e41f4b71Sopenharmony_ci  build() {
43e41f4b71Sopenharmony_ci    Row() {
44e41f4b71Sopenharmony_ci      Child({ regular_value: this.message, state_value: this.message, provide_value: this.message, initMessage: this.message, message: this.message,
45e41f4b71Sopenharmony_ci        buildTest: this.buildTest, initBuildTest: this.buildTest })
46e41f4b71Sopenharmony_ci    }
47e41f4b71Sopenharmony_ci  }
48e41f4b71Sopenharmony_ci}
49e41f4b71Sopenharmony_ci
50e41f4b71Sopenharmony_ci@Component
51e41f4b71Sopenharmony_cistruct Child {
52e41f4b71Sopenharmony_ci  @Builder buildFunction() {
53e41f4b71Sopenharmony_ci    Column() {
54e41f4b71Sopenharmony_ci      Text('initBuilderParam')
55e41f4b71Sopenharmony_ci        .fontSize(30)
56e41f4b71Sopenharmony_ci    }
57e41f4b71Sopenharmony_ci  }
58e41f4b71Sopenharmony_ci  @Require regular_value: string = 'Hello';
59e41f4b71Sopenharmony_ci  @Require @State state_value: string = "Hello";
60e41f4b71Sopenharmony_ci  @Require @Provide provide_value: string = "Hello";
61e41f4b71Sopenharmony_ci  @Require @BuilderParam buildTest: () => void;
62e41f4b71Sopenharmony_ci  @Require @BuilderParam initBuildTest: () => void = this.buildFunction;
63e41f4b71Sopenharmony_ci  @Require @Prop initMessage: string = 'Hello';
64e41f4b71Sopenharmony_ci  @Require @Prop message: string;
65e41f4b71Sopenharmony_ci
66e41f4b71Sopenharmony_ci  build() {
67e41f4b71Sopenharmony_ci    Column() {
68e41f4b71Sopenharmony_ci      Text(this.initMessage)
69e41f4b71Sopenharmony_ci        .fontSize(30)
70e41f4b71Sopenharmony_ci      Text(this.message)
71e41f4b71Sopenharmony_ci        .fontSize(30)
72e41f4b71Sopenharmony_ci      this.initBuildTest();
73e41f4b71Sopenharmony_ci      this.buildTest();
74e41f4b71Sopenharmony_ci    }
75e41f4b71Sopenharmony_ci    .width('100%')
76e41f4b71Sopenharmony_ci    .height('100%')
77e41f4b71Sopenharmony_ci  }
78e41f4b71Sopenharmony_ci}
79e41f4b71Sopenharmony_ci```
80e41f4b71Sopenharmony_ci
81e41f4b71Sopenharmony_ci ![img](figures/9e2d58bc-b0e1-4613-934b-8e4237bd5c05.png) 
82e41f4b71Sopenharmony_ci
83e41f4b71Sopenharmony_ci使用@ComponentV2修饰的自定义组组件ChildPage通过父组件ParentPage进行初始化,因为有@Require装饰,所以父组件必须进行构造赋值。
84e41f4b71Sopenharmony_ci
85e41f4b71Sopenharmony_ci```ts
86e41f4b71Sopenharmony_ci@ObservedV2
87e41f4b71Sopenharmony_ciclass Info {
88e41f4b71Sopenharmony_ci  @Trace name: string = '';
89e41f4b71Sopenharmony_ci  @Trace age: number = 0;
90e41f4b71Sopenharmony_ci}
91e41f4b71Sopenharmony_ci
92e41f4b71Sopenharmony_ci@ComponentV2
93e41f4b71Sopenharmony_cistruct ChildPage {
94e41f4b71Sopenharmony_ci  @Require @Param childInfo: Info = new Info();
95e41f4b71Sopenharmony_ci  @Require @Param state_value: string = "Hello";
96e41f4b71Sopenharmony_ci  build() {
97e41f4b71Sopenharmony_ci    Column() {
98e41f4b71Sopenharmony_ci      Text(`ChildPage childInfo name :${this.childInfo.name}`)
99e41f4b71Sopenharmony_ci        .fontSize(20)
100e41f4b71Sopenharmony_ci        .fontWeight(FontWeight.Bold)
101e41f4b71Sopenharmony_ci      Text(`ChildPage childInfo age :${this.childInfo.age}`)
102e41f4b71Sopenharmony_ci        .fontSize(20)
103e41f4b71Sopenharmony_ci        .fontWeight(FontWeight.Bold)
104e41f4b71Sopenharmony_ci      Text(`ChildPage state_value age :${this.state_value}`)
105e41f4b71Sopenharmony_ci        .fontSize(20)
106e41f4b71Sopenharmony_ci        .fontWeight(FontWeight.Bold)
107e41f4b71Sopenharmony_ci    }
108e41f4b71Sopenharmony_ci  }
109e41f4b71Sopenharmony_ci}
110e41f4b71Sopenharmony_ci
111e41f4b71Sopenharmony_ci@Entry
112e41f4b71Sopenharmony_ci@ComponentV2
113e41f4b71Sopenharmony_cistruct ParentPage {
114e41f4b71Sopenharmony_ci  info1: Info = { name: "Tom", age: 25 };
115e41f4b71Sopenharmony_ci  label1: string = "Hello World";
116e41f4b71Sopenharmony_ci  @Local info2: Info = { name: "Tom", age: 25 };
117e41f4b71Sopenharmony_ci  @Local label2: string = "Hello World";
118e41f4b71Sopenharmony_ci
119e41f4b71Sopenharmony_ci  build() {
120e41f4b71Sopenharmony_ci    Column() {
121e41f4b71Sopenharmony_ci      Text(`info1: ${this.info1.name}  ${this.info1.age}`) // Text1
122e41f4b71Sopenharmony_ci        .fontSize(30)
123e41f4b71Sopenharmony_ci        .fontWeight(FontWeight.Bold)
124e41f4b71Sopenharmony_ci      ChildPage({ childInfo: this.info1, state_value: this.label1}) // 调用自定义组件
125e41f4b71Sopenharmony_ci      Line()
126e41f4b71Sopenharmony_ci        .width('100%')
127e41f4b71Sopenharmony_ci        .height(5)
128e41f4b71Sopenharmony_ci        .backgroundColor('#000000').margin(10)
129e41f4b71Sopenharmony_ci      Text(`info2: ${this.info2.name}  ${this.info2.age}`) // Text2
130e41f4b71Sopenharmony_ci        .fontSize(30)
131e41f4b71Sopenharmony_ci        .fontWeight(FontWeight.Bold)
132e41f4b71Sopenharmony_ci      ChildPage({ childInfo: this.info2, state_value: this.label2}) // 调用自定义组件
133e41f4b71Sopenharmony_ci      Line()
134e41f4b71Sopenharmony_ci        .width('100%')
135e41f4b71Sopenharmony_ci        .height(5)
136e41f4b71Sopenharmony_ci        .backgroundColor('#000000').margin(10)
137e41f4b71Sopenharmony_ci      Button("change info1&info2")
138e41f4b71Sopenharmony_ci        .onClick(() => {
139e41f4b71Sopenharmony_ci          this.info1 = { name: "Cat", age: 18} // Text1不会刷新,原因是没有装饰器修饰监听不到值的改变。
140e41f4b71Sopenharmony_ci          this.info2 = { name: "Cat", age: 18} // Text2会刷新,原因是有装饰器修饰,可以监听到值的改变。
141e41f4b71Sopenharmony_ci          this.label1 = "Luck"; // 不会刷新,原因是没有装饰器修饰监听不到值的改变。
142e41f4b71Sopenharmony_ci          this.label2 = "Luck"; // 会刷新,原因是有装饰器修饰,可以监听到值的改变。
143e41f4b71Sopenharmony_ci        })
144e41f4b71Sopenharmony_ci    }
145e41f4b71Sopenharmony_ci  }
146e41f4b71Sopenharmony_ci}
147e41f4b71Sopenharmony_ci```
148e41f4b71Sopenharmony_ci
149e41f4b71Sopenharmony_ci## 错误场景
150e41f4b71Sopenharmony_ci
151e41f4b71Sopenharmony_ci```ts
152e41f4b71Sopenharmony_ci@Entry
153e41f4b71Sopenharmony_ci@Component
154e41f4b71Sopenharmony_cistruct Index {
155e41f4b71Sopenharmony_ci  @State message: string = 'Hello World';
156e41f4b71Sopenharmony_ci
157e41f4b71Sopenharmony_ci  @Builder buildTest() {
158e41f4b71Sopenharmony_ci    Row() {
159e41f4b71Sopenharmony_ci      Text('Hello, world')
160e41f4b71Sopenharmony_ci        .fontSize(30)
161e41f4b71Sopenharmony_ci    }
162e41f4b71Sopenharmony_ci  }
163e41f4b71Sopenharmony_ci
164e41f4b71Sopenharmony_ci  build() {
165e41f4b71Sopenharmony_ci    Row() {
166e41f4b71Sopenharmony_ci      Child()
167e41f4b71Sopenharmony_ci    }
168e41f4b71Sopenharmony_ci  }
169e41f4b71Sopenharmony_ci}
170e41f4b71Sopenharmony_ci
171e41f4b71Sopenharmony_ci@Component
172e41f4b71Sopenharmony_cistruct Child {
173e41f4b71Sopenharmony_ci  @Builder buildFunction() {
174e41f4b71Sopenharmony_ci    Column() {
175e41f4b71Sopenharmony_ci      Text('initBuilderParam')
176e41f4b71Sopenharmony_ci        .fontSize(30)
177e41f4b71Sopenharmony_ci    }
178e41f4b71Sopenharmony_ci  }
179e41f4b71Sopenharmony_ci  // 使用@Require必须构造时传参。
180e41f4b71Sopenharmony_ci  @Require regular_value: string = 'Hello';
181e41f4b71Sopenharmony_ci  @Require @State state_value: string = "Hello";
182e41f4b71Sopenharmony_ci  @Require @Provide provide_value: string = "Hello";
183e41f4b71Sopenharmony_ci  @Require @BuilderParam initBuildTest: () => void = this.buildFunction;
184e41f4b71Sopenharmony_ci  @Require @Prop initMessage: string = 'Hello';
185e41f4b71Sopenharmony_ci
186e41f4b71Sopenharmony_ci  build() {
187e41f4b71Sopenharmony_ci    Column() {
188e41f4b71Sopenharmony_ci      Text(this.initMessage)
189e41f4b71Sopenharmony_ci        .fontSize(30)
190e41f4b71Sopenharmony_ci      this.initBuildTest();
191e41f4b71Sopenharmony_ci    }
192e41f4b71Sopenharmony_ci  }
193e41f4b71Sopenharmony_ci}
194e41f4b71Sopenharmony_ci```
195e41f4b71Sopenharmony_ci
196