1e41f4b71Sopenharmony_ci# Declarative UI Description
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ci
4e41f4b71Sopenharmony_ciArkTS declaratively combines and extends components to describe the UI of an application. It also provides basic methods for configuring attributes, events, and child components to help you implement application interaction logic.
5e41f4b71Sopenharmony_ci
6e41f4b71Sopenharmony_ci
7e41f4b71Sopenharmony_ci## Creating a Component
8e41f4b71Sopenharmony_ci
9e41f4b71Sopenharmony_ciDepending on the builder, you can create components with or without mandatory parameters.
10e41f4b71Sopenharmony_ci
11e41f4b71Sopenharmony_ci>  **NOTE**
12e41f4b71Sopenharmony_ci>
13e41f4b71Sopenharmony_ci>  The **new** operator is not required when you create a component.
14e41f4b71Sopenharmony_ci
15e41f4b71Sopenharmony_ci
16e41f4b71Sopenharmony_ci### Without Mandatory Parameters
17e41f4b71Sopenharmony_ci
18e41f4b71Sopenharmony_ciA struct without mandatory parameters is a component whose API definition has empty parentheses. No parameter needs to be passed to this type of component, for example, the **Divider** component in the following snippet:
19e41f4b71Sopenharmony_ci
20e41f4b71Sopenharmony_ci
21e41f4b71Sopenharmony_ci```ts
22e41f4b71Sopenharmony_ciColumn() {
23e41f4b71Sopenharmony_ci  Text('item 1')
24e41f4b71Sopenharmony_ci  Divider()
25e41f4b71Sopenharmony_ci  Text('item 2')
26e41f4b71Sopenharmony_ci}
27e41f4b71Sopenharmony_ci```
28e41f4b71Sopenharmony_ci
29e41f4b71Sopenharmony_ci
30e41f4b71Sopenharmony_ci### With Mandatory Parameters
31e41f4b71Sopenharmony_ci
32e41f4b71Sopenharmony_ciA struct with mandatory parameters is a component whose API definition expects parameters enclosed in parentheses.
33e41f4b71Sopenharmony_ci
34e41f4b71Sopenharmony_ci- Set the mandatory parameter **src** of the **\<Image>** component as follows:
35e41f4b71Sopenharmony_ci
36e41f4b71Sopenharmony_ci  ```ts
37e41f4b71Sopenharmony_ci  Image('https://xyz/test.jpg')
38e41f4b71Sopenharmony_ci  ```
39e41f4b71Sopenharmony_ci
40e41f4b71Sopenharmony_ci
41e41f4b71Sopenharmony_ci- Set the optional parameter **content** of the **\<Text>** component.
42e41f4b71Sopenharmony_ci
43e41f4b71Sopenharmony_ci  ```ts
44e41f4b71Sopenharmony_ci  // Parameter of the string type
45e41f4b71Sopenharmony_ci  Text('test')
46e41f4b71Sopenharmony_ci  // Add application resources in $r format, which can be used in multi-language scenarios.
47e41f4b71Sopenharmony_ci  Text($r('app.string.title_value'))
48e41f4b71Sopenharmony_ci  // No mandatory parameters
49e41f4b71Sopenharmony_ci  Text()
50e41f4b71Sopenharmony_ci  ```
51e41f4b71Sopenharmony_ci
52e41f4b71Sopenharmony_ci
53e41f4b71Sopenharmony_ci- You can also use variables or expressions to assign values to parameters. The result type returned by an expression must meet the parameter type requirements.
54e41f4b71Sopenharmony_ci    For example, to set a variable or expression to construct the **\<Image>** and **\<Text>** components:
55e41f4b71Sopenharmony_ci
56e41f4b71Sopenharmony_ci    ```ts
57e41f4b71Sopenharmony_ci    Image(this.imagePath)
58e41f4b71Sopenharmony_ci    Image('https://' + this.imageUrl)
59e41f4b71Sopenharmony_ci    Text(`count: ${this.count}`)
60e41f4b71Sopenharmony_ci    ```
61e41f4b71Sopenharmony_ci
62e41f4b71Sopenharmony_ci
63e41f4b71Sopenharmony_ci## Configuring Attributes
64e41f4b71Sopenharmony_ci
65e41f4b71Sopenharmony_ciUse chainable attribute methods to configure the style and other attributes of built-in components. It is recommended that a separate line be used for each attribute method.
66e41f4b71Sopenharmony_ci
67e41f4b71Sopenharmony_ci
68e41f4b71Sopenharmony_ci- Example of configuring the **fontSize** attribute for the **\<Text>** component:
69e41f4b71Sopenharmony_ci
70e41f4b71Sopenharmony_ci  ```ts
71e41f4b71Sopenharmony_ci  Text('test')
72e41f4b71Sopenharmony_ci    .fontSize(12)
73e41f4b71Sopenharmony_ci  ```
74e41f4b71Sopenharmony_ci
75e41f4b71Sopenharmony_ci- Example of configuring multiple attributes for the **\<Image>** component:
76e41f4b71Sopenharmony_ci
77e41f4b71Sopenharmony_ci  ```ts
78e41f4b71Sopenharmony_ci  Image('test.jpg')
79e41f4b71Sopenharmony_ci    .alt('error.jpg')    
80e41f4b71Sopenharmony_ci    .width(100)    
81e41f4b71Sopenharmony_ci    .height(100)
82e41f4b71Sopenharmony_ci  ```
83e41f4b71Sopenharmony_ci
84e41f4b71Sopenharmony_ci- Attribute methods accept expressions and variables as well constant parameters.
85e41f4b71Sopenharmony_ci
86e41f4b71Sopenharmony_ci  ```ts
87e41f4b71Sopenharmony_ci  Text('hello')
88e41f4b71Sopenharmony_ci    .fontSize(this.size)
89e41f4b71Sopenharmony_ci  Image('test.jpg')
90e41f4b71Sopenharmony_ci    .width(this.count % 2 === 0 ? 100 : 200)    
91e41f4b71Sopenharmony_ci    .height(this.offset + 100)
92e41f4b71Sopenharmony_ci  ```
93e41f4b71Sopenharmony_ci
94e41f4b71Sopenharmony_ci- For built-in components, ArkUI also predefines some enumeration types. These enumeration types can be passed as parameters, as long as they meet the parameter type requirements.
95e41f4b71Sopenharmony_ci  Example of configuring the font color and style of the **\<Text>** component:
96e41f4b71Sopenharmony_ci
97e41f4b71Sopenharmony_ci  ```ts
98e41f4b71Sopenharmony_ci  Text('hello')
99e41f4b71Sopenharmony_ci    .fontSize(20)
100e41f4b71Sopenharmony_ci    .fontColor(Color.Red)
101e41f4b71Sopenharmony_ci    .fontWeight(FontWeight.Bold)
102e41f4b71Sopenharmony_ci  ```
103e41f4b71Sopenharmony_ci
104e41f4b71Sopenharmony_ci
105e41f4b71Sopenharmony_ci## Handling Events
106e41f4b71Sopenharmony_ci
107e41f4b71Sopenharmony_ciUse chainable event methods to configure events supported by built-in components. It is recommended that a separate line be used for each event method.
108e41f4b71Sopenharmony_ci
109e41f4b71Sopenharmony_ci
110e41f4b71Sopenharmony_ci- Example of using an arrow function expression to configure the event of a component:
111e41f4b71Sopenharmony_ci
112e41f4b71Sopenharmony_ci  ```ts
113e41f4b71Sopenharmony_ci  Button('Click me')
114e41f4b71Sopenharmony_ci    .onClick(() => {
115e41f4b71Sopenharmony_ci      this.myText = 'ArkUI';
116e41f4b71Sopenharmony_ci    })
117e41f4b71Sopenharmony_ci  ```
118e41f4b71Sopenharmony_ci
119e41f4b71Sopenharmony_ci- Example of using an arrow function expression to configure the event of a component (**() => {...}** must be used to ensure that the function is bound to the component and complies with the ArkTS syntax specifications):
120e41f4b71Sopenharmony_ci
121e41f4b71Sopenharmony_ci  ```ts
122e41f4b71Sopenharmony_ci  Button('add counter')
123e41f4b71Sopenharmony_ci    .onClick(() => {
124e41f4b71Sopenharmony_ci      this.counter += 2;
125e41f4b71Sopenharmony_ci    })
126e41f4b71Sopenharmony_ci  ```
127e41f4b71Sopenharmony_ci
128e41f4b71Sopenharmony_ci- Example of using a component's member function to configure the event of the component, where **this** binding is needed: (This usage is not recommended in ArkTS.)
129e41f4b71Sopenharmony_ci
130e41f4b71Sopenharmony_ci  ```ts
131e41f4b71Sopenharmony_ci  myClickHandler(): void {
132e41f4b71Sopenharmony_ci    this.counter += 2;
133e41f4b71Sopenharmony_ci  }
134e41f4b71Sopenharmony_ci  ...
135e41f4b71Sopenharmony_ci  Button('add counter')
136e41f4b71Sopenharmony_ci    .onClick(this.myClickHandler.bind(this))
137e41f4b71Sopenharmony_ci  ```
138e41f4b71Sopenharmony_ci
139e41f4b71Sopenharmony_ci- Example of using an arrow function expression for a declaration, where **this** binding is not needed:
140e41f4b71Sopenharmony_ci
141e41f4b71Sopenharmony_ci  ```ts
142e41f4b71Sopenharmony_ci  fn = () => {
143e41f4b71Sopenharmony_ci    console.info(`counter: ${this.counter}`)
144e41f4b71Sopenharmony_ci    this.counter++
145e41f4b71Sopenharmony_ci  }
146e41f4b71Sopenharmony_ci  ...
147e41f4b71Sopenharmony_ci  Button('add counter')
148e41f4b71Sopenharmony_ci    .onClick(this.fn)
149e41f4b71Sopenharmony_ci  ```
150e41f4b71Sopenharmony_ci
151e41f4b71Sopenharmony_ci> **NOTE**
152e41f4b71Sopenharmony_ci>
153e41f4b71Sopenharmony_ci> In arrow functions, **this** inherits its value from the surrounding (lexical) scope in which they are defined. This means that, in anonymous functions, **this** may present an unclear reference and is therefore not allowed in ArkTS.
154e41f4b71Sopenharmony_ci
155e41f4b71Sopenharmony_ci
156e41f4b71Sopenharmony_ci## Configuring Child Components
157e41f4b71Sopenharmony_ci
158e41f4b71Sopenharmony_ciFor a component with child components, for example, a container component, add the UI descriptions of the child components inside parentheses. The **\<Column>**, **\<Row>**, **\<Stack>**, **\<Grid>**, and **\<List>** components are all container components.
159e41f4b71Sopenharmony_ci
160e41f4b71Sopenharmony_ci
161e41f4b71Sopenharmony_ci- Simple example of configuring child components for the **\<Column>** component:
162e41f4b71Sopenharmony_ci
163e41f4b71Sopenharmony_ci  ```ts
164e41f4b71Sopenharmony_ci  Column() {
165e41f4b71Sopenharmony_ci    Text('Hello')
166e41f4b71Sopenharmony_ci      .fontSize(100)
167e41f4b71Sopenharmony_ci    Divider()
168e41f4b71Sopenharmony_ci    Text(this.myText)
169e41f4b71Sopenharmony_ci      .fontSize(100)
170e41f4b71Sopenharmony_ci      .fontColor(Color.Red)
171e41f4b71Sopenharmony_ci  }
172e41f4b71Sopenharmony_ci  ```
173e41f4b71Sopenharmony_ci
174e41f4b71Sopenharmony_ci- Example of nested child components in the **\<Column>** component:.
175e41f4b71Sopenharmony_ci
176e41f4b71Sopenharmony_ci  ```ts
177e41f4b71Sopenharmony_ci  Column() {
178e41f4b71Sopenharmony_ci    Row() {
179e41f4b71Sopenharmony_ci      Image('test1.jpg')
180e41f4b71Sopenharmony_ci        .width(100)
181e41f4b71Sopenharmony_ci        .height(100)
182e41f4b71Sopenharmony_ci      Button('click +1')
183e41f4b71Sopenharmony_ci        .onClick(() => {
184e41f4b71Sopenharmony_ci          console.info('+1 clicked!');
185e41f4b71Sopenharmony_ci        })
186e41f4b71Sopenharmony_ci    }
187e41f4b71Sopenharmony_ci  }
188e41f4b71Sopenharmony_ci  ```
189