1# \@Extend装饰器:定义扩展组件样式
2
3
4在前文的示例中,可以使用[\@Styles](arkts-style.md)用于样式的重用,在\@Styles的基础上,我们提供了\@Extend,用于扩展原生组件样式。
5
6
7> **说明:**
8>
9> 从API version 9开始,该装饰器支持在ArkTS卡片中使用。
10>
11> 从API version 11开始,该装饰器支持在原子化服务中使用。
12
13## 装饰器使用说明
14
15
16### 语法
17
18
19```ts
20@Extend(UIComponentName) function functionName { ... }
21```
22
23
24### 使用规则
25
26- 和\@Styles不同,\@Extend支持封装指定组件的私有属性、私有事件和自身定义的全局方法。
27
28  ```ts
29  // @Extend(Text)可以支持Text的私有属性fontColor
30  @Extend(Text) function fancy () {
31    .fontColor(Color.Red)
32  }
33  // superFancyText可以调用预定义的fancy
34  @Extend(Text) function superFancyText(size:number) {
35      .fontSize(size)
36      .fancy()
37  }
38  ```
39
40
41- 和\@Styles不同,\@Extend装饰的方法支持参数,开发者可以在调用时传递参数,调用遵循TS方法传值调用。
42
43  ```ts
44  // xxx.ets
45  @Extend(Text) function fancy (fontSize: number) {
46    .fontColor(Color.Red)
47    .fontSize(fontSize)
48  }
49
50  @Entry
51  @Component
52  struct FancyUse {
53    build() {
54      Row({ space: 10 }) {
55        Text('Fancy')
56          .fancy(16)
57        Text('Fancy')
58          .fancy(24)
59      }
60    }
61  }
62  ```
63
64- \@Extend装饰的方法的参数可以为function,作为Event事件的句柄。
65
66  ```ts
67  @Extend(Text) function makeMeClick(onClick: () => void) {
68    .backgroundColor(Color.Blue)
69    .onClick(onClick)
70  }
71
72  @Entry
73  @Component
74  struct FancyUse {
75    @State label: string = 'Hello World';
76
77    onClickHandler() {
78      this.label = 'Hello ArkUI';
79    }
80
81    build() {
82      Row({ space: 10 }) {
83        Text(`${this.label}`)
84          .makeMeClick(() => {this.onClickHandler()})
85      }
86    }
87  }
88  ```
89
90- \@Extend的参数可以为[状态变量](arkts-state-management-overview.md),当状态变量改变时,UI可以正常的被刷新渲染。
91
92  ```ts
93  @Extend(Text) function fancy (fontSize: number) {
94    .fontColor(Color.Red)
95    .fontSize(fontSize)
96  }
97
98  @Entry
99  @Component
100  struct FancyUse {
101    @State fontSizeValue: number = 20
102    build() {
103      Row({ space: 10 }) {
104        Text('Fancy')
105          .fancy(this.fontSizeValue)
106          .onClick(() => {
107            this.fontSizeValue = 30
108          })
109      }
110    }
111  }
112  ```
113
114
115## 限制条件
116
117- 和\@Styles不同,\@Extend仅支持在全局定义,不支持在组件内部定义。
118
119> **说明:**
120>
121> 只能在当前文件内使用,不支持export。
122>
123> 如果想实现export功能,推荐使用[AttributeModifier](../ui/arkts-user-defined-extension-attributeModifier.md)。
124
125【反例】
126
127```ts
128@Entry
129@Component
130struct FancyUse {
131  // 错误写法,@Extend仅支持在全局定义,不支持在组件内部定义
132  @Extend(Text) function fancy (fontSize: number) {
133    .fontSize(fontSize)
134  }
135
136  build() {
137    Row({ space: 10 }) {
138      Text('Fancy')
139        .fancy(16)
140    }
141  }
142}
143```
144
145【正例】
146
147```ts
148// 正确写法
149@Extend(Text) function fancy (fontSize: number) {
150  .fontSize(fontSize)
151}
152
153@Entry
154@Component
155struct FancyUse {
156
157  build() {
158    Row({ space: 10 }) {
159      Text('Fancy')
160        .fancy(16)
161    }
162  }
163}
164```
165
166
167## 使用场景
168
169以下示例声明了3个Text组件,每个Text组件均设置了fontStyle、fontWeight和backgroundColor样式。
170
171
172```ts
173@Entry
174@Component
175struct FancyUse {
176  @State label: string = 'Hello World'
177
178  build() {
179    Row({ space: 10 }) {
180      Text(`${this.label}`)
181        .fontStyle(FontStyle.Italic)
182        .fontWeight(100)
183        .backgroundColor(Color.Blue)
184      Text(`${this.label}`)
185        .fontStyle(FontStyle.Italic)
186        .fontWeight(200)
187        .backgroundColor(Color.Pink)
188      Text(`${this.label}`)
189        .fontStyle(FontStyle.Italic)
190        .fontWeight(300)
191        .backgroundColor(Color.Orange)
192    }.margin('20%')
193  }
194}
195```
196
197\@Extend将样式组合复用,示例如下。
198
199
200```ts
201@Extend(Text) function fancyText(weightValue: number, color: Color) {
202  .fontStyle(FontStyle.Italic)
203  .fontWeight(weightValue)
204  .backgroundColor(color)
205}
206```
207
208通过\@Extend组合样式后,使得代码更加简洁,增强可读性。
209
210
211```ts
212@Entry
213@Component
214struct FancyUse {
215  @State label: string = 'Hello World'
216
217  build() {
218    Row({ space: 10 }) {
219      Text(`${this.label}`)
220        .fancyText(100, Color.Blue)
221      Text(`${this.label}`)
222        .fancyText(200, Color.Pink)
223      Text(`${this.label}`)
224        .fancyText(300, Color.Orange)
225    }.margin('20%')
226  }
227}
228```
229