1# Building the First ArkTS Application in Stage Model
2
3
4> **NOTE**
5>
6> In this document, DevEco Studio 4.1 Beta1 is used.<!--Del--> You can download it [here](../../release-notes/OpenHarmony-v4.1-beta1.md#version-mapping).<!--DelEnd-->
7
8## Creating an ArkTS Project
9
101. If you are opening DevEco Studio for the first time, click **Create Project**. If a project is already open, choose **File** > **New** > **Create Project** from the menu bar.
11
122. On the **Choose Your Ability Template** page, select **Application** (or **Atomic Service**, depending on your project), select **[OpenHarmony]Empty Ability** as the template, and click **Next**.
13
14   ![createProject](figures/createProject.png)
15
163. On the project configuration page, set **Compile SDK** to **11** and retain the default values for other parameters.
17
18   The **Node** parameter sets the Node.js version to use for the project. You can use an existing version or download a new one.
19
20   ![chooseStageModel](figures/chooseStageModel.png)
21
224. Click **Finish**. DevEco Studio will automatically generate the sample code and resources that match your project type. Wait until the project is created.
23
24
25## ArkTS Project Directory Structure (Stage Model)
26
27![project](figures/project.png)
28
29- **AppScope &gt; app.json5**: application-level configuration information. For details, see [app.json5 Configuration File](app-configuration-file.md).
30
31- **entry**: OpenHarmony project module, which can be built into an ability package (HAP).
32  - **src > main > ets**: a collection of ArkTS source code.
33
34  - **src > main > ets > entryability**: entry to your application/service.
35
36  - **src > main > ets > pages**: pages included in your application/service.
37
38  - **src > main > resources**: a collection of resource files used by your application/service, such as graphics, multimedia, character strings, and layout files. For details about resource files, see [Resource Categories and Access](resource-categories-and-access.md#resource-categories).
39
40  - **src > main > module.json5**: module configuration file. This file describes the global configuration information of the application/service, the device-specific configuration information, and the configuration information of the HAP file. For details, see [module.json5 Configuration File](module-configuration-file.md).
41
42  - **build-profile.json5**: current module information and build configuration options, including **buildOption** and **targets**.
43
44  - **hvigorfile.ts**: module-level build script. You can customize related tasks and code implementation in this file.
45  - **obfuscation-rules.txt**: obfuscation rule file. When obfuscation is enabled, DevEco Studio compiles, obfuscates, and compresses code during builds in Release mode.
46
47- **oh_modules**: third-party library dependency information.
48
49- **build-profile.json5**: application-level configuration information, including the **signingConfigs** and **products** configuration.
50
51- **hvigorfile.ts**: application-level build script.
52
53
54## Building the First Page
55
561. Use the **Text** component.
57
58   After the project synchronization is complete, choose **entry** > **src** > **main** > **ets** > **pages** in the **Project** window and open the **Index.ets** file. You can see that the file contains a **Text** component. The sample code in the **Index.ets** file is shown below:
59
60   ```ts
61   // Index.ets
62   @Entry
63   @Component
64   struct Index {
65     @State message: string = 'Hello World';
66
67     build() {
68       Row() {
69         Column() {
70           Text(this.message)
71             .fontSize(50)
72             .fontWeight(FontWeight.Bold)
73         }
74         .width('100%')
75       }
76       .height('100%')
77     }
78   }
79   ```
80
812. Add a **Button** component.
82
83   On the default page, add a **Button** component to respond to user clicks and implement redirection to another page. The sample code in the **Index.ets** file is shown below:
84
85   ```ts
86   // Index.ets
87   @Entry
88   @Component
89   struct Index {
90     @State message: string = 'Hello World';
91
92     build() {
93       Row() {
94         Column() {
95           Text(this.message)
96             .fontSize(50)
97             .fontWeight(FontWeight.Bold)
98           // Add a button to respond to user clicks.
99           Button() {
100             Text('Next')
101               .fontSize(30)
102               .fontWeight(FontWeight.Bold)
103           }
104           .type(ButtonType.Capsule)
105           .margin({
106             top: 20
107           })
108           .backgroundColor('#0D9FFB')
109           .width('40%')
110           .height('5%')
111         }
112         .width('100%')
113       }
114       .height('100%')
115     }
116   }
117   ```
118
1193. On the toolbar in the upper right corner of the editing window, click **Previewer**. Below is how the first page looks in the Previewer.
120
121   ![en-us_image_0000001311334976](figures/en-us_image_0000001311334976.png)
122
123
124## Building the Second Page
125
1261. Create the second page.
127
128   - Create the second page file: In the **Project** window, choose **entry** > **src** > **main** > **ets**. Right-click the **pages** folder, choose **New** > **ArkTS File**, name the page **Second**, and click **Finish**. Below is the structure of the **Second** folder.
129
130      ![secondPage](figures/secondPage.png)
131
132      >  **NOTE**
133      >
134      > You can also right-click the **pages** folder and choose **New** > **Page** from the shortcut menu. In this scenario, you do not need to manually configure page routes.
135   - Configure the route for the second page: In the **Project** window, choose **entry** > **src** > **main** > **resources** > **base** > **profile**. In the **main_pages.json** file, set **pages/Second** under **src**. The sample code is as follows:
136
137      ```json
138      {
139        "src": [
140          "pages/Index",
141          "pages/Second"
142        ]
143      }
144      ```
145
1462. Add **Text** and **Button** components.
147
148   Add **Text** and **Button** components and set their styles, by referring to the first page. The sample code in the **Second.ets** file is shown below:
149
150   ```ts
151   // Second.ets
152   @Entry
153   @Component
154   struct Second {
155     @State message: string = 'Hi there';
156
157     build() {
158       Row() {
159         Column() {
160           Text(this.message)
161             .fontSize(50)
162             .fontWeight(FontWeight.Bold)
163           Button() {
164             Text('Back')
165               .fontSize(25)
166               .fontWeight(FontWeight.Bold)
167           }
168           .type(ButtonType.Capsule)
169           .margin({
170             top: 20
171           })
172           .backgroundColor('#0D9FFB')
173           .width('40%')
174           .height('5%')
175         }
176         .width('100%')
177       }
178       .height('100%')
179     }
180   }
181   ```
182
183
184## Implementing Page Redirection
185
186You can implement page redirection through the [page router](../reference/apis-arkui/js-apis-router.md), which finds the target page based on the page URL. Import the **router** module and then perform the steps below.
187
188To deliver better transition effects, use [Navigation](../ui/arkts-navigation-navigation.md).
189
1901. Implement redirection from the first page to the second page.
191
192   In the **Index.ets** file of the first page, bind the **onClick** event to the **Next** button so that clicking the button redirects the user to the second page. The sample code in the **Index.ets** file is shown below:
193
194   ```ts
195   // Index.ets
196   // Import the router module.
197   import { router } from '@kit.ArkUI';
198   import { BusinessError } from '@kit.BasicServicesKit';
199
200   @Entry
201   @Component
202   struct Index {
203     @State message: string = 'Hello World';
204
205     build() {
206       Row() {
207         Column() {
208           Text(this.message)
209             .fontSize(50)
210             .fontWeight(FontWeight.Bold)
211           // Add a button to respond to user clicks.
212           Button() {
213             Text('Next')
214               .fontSize(30)
215               .fontWeight(FontWeight.Bold)
216           }
217           .type(ButtonType.Capsule)
218           .margin({
219             top: 20
220           })
221           .backgroundColor('#0D9FFB')
222           .width('40%')
223           .height('5%')
224           // Bind the onClick event to the Next button so that clicking the button redirects the user to the second page.
225           .onClick(() => {
226             console.info(`Succeeded in clicking the 'Next' button.`)
227            // Go to the second page.
228              router.pushUrl({ url: 'pages/Second' }).then(() => {
229                console.info('Succeeded in jumping to the second page.')
230              }).catch((err: BusinessError) => {
231                console.error(`Failed to jump to the second page.Code is ${err.code}, message is ${err.message}`)
232              })
233           })
234         }
235         .width('100%')
236       }
237       .height('100%')
238     }
239   }
240   ```
241
2422. Implement redirection from the second page to the first page.
243
244   In the **Second.ets** file of the second page, bind the **onClick** event to the **Back** button so that clicking the button redirects the user back to the first page. The sample code in the **Second.ets** file is shown below:
245
246   ```ts
247   // Second.ets
248   // Import the router module.
249   import { router } from '@kit.ArkUI';
250   import { BusinessError } from '@kit.BasicServicesKit';
251
252   @Entry
253   @Component
254   struct Second {
255     @State message: string = 'Hi there';
256
257     build() {
258       Row() {
259         Column() {
260           Text(this.message)
261             .fontSize(50)
262             .fontWeight(FontWeight.Bold)
263           Button() {
264             Text('Back')
265               .fontSize(25)
266               .fontWeight(FontWeight.Bold)
267           }
268           .type(ButtonType.Capsule)
269           .margin({
270             top: 20
271           })
272           .backgroundColor('#0D9FFB')
273           .width('40%')
274           .height('5%')
275           // Bind the onClick event to the Back button so that clicking the button redirects the user back to the first page.
276           .onClick(() => {
277             console.info(`Succeeded in clicking the 'Back' button.`)
278             try {
279               // Return to the first page.
280               router.back()
281               console.info('Succeeded in returning to the first page.')
282             } catch (err) {
283                let code = (err as BusinessError).code;
284                let message = (err as BusinessError).message;
285               console.error(`Failed to return to the first page.Code is ${code}, message is ${message}`)
286             }
287           })
288         }
289         .width('100%')
290       }
291       .height('100%')
292     }
293   }
294   ```
295
2963. Open the **Index.ets** file and click ![en-us_image_0000001311015192](figures/en-us_image_0000001311015192.png) in the Previewer to refresh the file. The display effect is shown in the figure below.
297
298   ![en-us_image_0000001364254729](figures/en-us_image_0000001364254729.png)
299
300
301## Running the Application on a Real Device
302
3031. Connect the development board running the OpenHarmony standard system to the computer.
304
3052. Choose **File** > **Project Structure...** > **Project** > **SigningConfigs**, and select **Automatically generate signature**. Wait until the automatic signing is complete, and click **OK**. See the following figure.
306
307   ![signConfig](figures/signConfig.png)
308
3093. On the toolbar in the upper right corner of the editing window, click ![en-us_image_0000001364054485](figures/en-us_image_0000001364054485.png). The display effect is shown in the figure below.
310
311   ![en-us_image_0000001364254729](figures/en-us_image_0000001364254729.png)
312
313Congratulations! You have finished developing your OpenHarmony application in ArkTS in the stage model.
314