1# Building the First ArkTS Application in Stage Model 2 3## Creating an ArkTS Project 4 5> **NOTE** 6> 7> - Since DevEco Studio 4.1 Beta1, you can only create a HarmonyOS project by default. To create an OpenHarmony project, you should modify some fields in the created HarmonyOS project. 8> 9> - To ensure the running effect, the latest [DevEco Studio](https://developer.huawei.com/consumer/en/download/) is used as an example. 10 111. 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. 12 132. On the **Choose Your Ability Template** page, select **Application** (or **Atomic Service**, depending on your project), select **Empty Ability** as the template, and click **Next**. 14 15 To develop native projects, select the Native C++ template. For details about how to use templates, see [Introduction to Project Templates](https://developer.huawei.com/consumer/en/doc/harmonyos-guides/ide-template). 16 17  18 193. On the **Create Project** page, **Compatible SDK** indicates the earliest compatible API version. Choose **5.0.0(12)** as an example and retain the default values for other parameters. 20 21  22 234. Click **Finish**. DevEco Studio will automatically generate the sample code and resources that match your project type. Wait until the HarmonyOS project is created. 24 255. After the project is created, perform the following operations to modify related fields in the project-level **build-profile.json5** file (at the same directory level as **entry**): 26 27 1. Add the **compileSdkVersion** field. 28 29 2. Set the value of **compatibleSdkVersion** and **compileSdkVersion** to an integer, such as **10**, **11**, or **12**. 30 31 3. Change the **runtimeOS** field from **HarmonyOS** to **OpenHarmony**. 32 33 ```json 34 "products": [ 35 { 36 "name": "default", 37 "signingConfig": "default", 38 "compileSdkVersion": 12, // Version for compiling the OpenHarmony application or atomic service. 39 "compatibleSdkVersion": 12, // Minimum version compatible with the OpenHarmony application or atomic service. 40 "runtimeOS": "OpenHarmony", 41 } 42 ], 43 ``` 44 456. Click **Sync Now** to start synchronization. 46 47 In the **Sync Check** dialog box, click **Yes** to switch the phone type in the **module.json5/config.json** file to the default type supported by the OpenHarmony, and delete other device types that are not applicable to the OpenHarmony. The OpenHarmony project is created if the synchronization is successful and no other error is reported. 48 49## ArkTS Project Directory Structure (Stage Model) 50 51 52 53- **AppScope > app.json5**: application-level configuration information. For details, see [app.json5 Configuration File](app-configuration-file.md). 54 55- **entry**: application/service module, which can be built into a HAP. 56 - **src > main > ets**: a collection of ArkTS source code. 57 58 - **src > main > ets > entryability**: entry to your application/service. 59 60 - **src > main > ets > pages**: pages included in your application/service. 61 62 - **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). 63 64 - **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). 65 66 - **build-profile.json5**: current module information and build configuration options, including **buildOption** and **targets**. 67 68 - **hvigorfile.ts**: module-level build script. You can customize related tasks and code implementation in this file. 69 - **obfuscation-rules.txt**: obfuscation rule file. When obfuscation is enabled, DevEco Studio compiles, obfuscates, and compresses code during builds in Release mode. 70 71- **oh_modules**: third-party library dependency information. 72 73- **build-profile.json5**: project-level configuration, including **signingConfigs** and **products**. 74 75- **hvigorfile.ts**: application-level build script. 76 77 78## Building the First Page 79 801. Use the **Text** component. 81 82 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: 83 84 ```ts 85 // Index.ets 86 @Entry 87 @Component 88 struct Index { 89 @State message: string = 'Hello World'; 90 91 build() { 92 Row() { 93 Column() { 94 Text(this.message) 95 .fontSize(50) 96 .fontWeight(FontWeight.Bold) 97 } 98 .width('100%') 99 } 100 .height('100%') 101 } 102 } 103 ``` 104 1052. Add a **Button** component. 106 107 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: 108 109 ```ts 110 // Index.ets 111 @Entry 112 @Component 113 struct Index { 114 @State message: string = 'Hello World'; 115 116 build() { 117 Row() { 118 Column() { 119 Text(this.message) 120 .fontSize(50) 121 .fontWeight(FontWeight.Bold) 122 // Add a button to respond to user clicks. 123 Button() { 124 Text('Next') 125 .fontSize(30) 126 .fontWeight(FontWeight.Bold) 127 } 128 .type(ButtonType.Capsule) 129 .margin({ 130 top: 20 131 }) 132 .backgroundColor('#0D9FFB') 133 .width('40%') 134 .height('5%') 135 } 136 .width('100%') 137 } 138 .height('100%') 139 } 140 } 141 ``` 142 1433. On the toolbar in the upper right corner of the editing window, click **Previewer**. Below is how the first page looks in the Previewer. 144 145  146 147 148## Building the Second Page 149 1501. Create the second page. 151 152 - 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 press **Enter**. Below is the structure of the **Second** folder. 153 154  155 156 > **NOTE** 157 > 158 > You can also right-click the **pages** folder, choose **New** > **Page** > **Empty Page** from the shortcut menu, name the page **Second**, and click **Finish**. This way, you can skip the step for manually configuring the route for the second page. 159 160 - 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: 161 162 ```json 163 { 164 "src": [ 165 "pages/Index", 166 "pages/Second" 167 ] 168 } 169 ``` 170 1712. Add **Text** and **Button** components. 172 173 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: 174 175 ```ts 176 // Second.ets 177 @Entry 178 @Component 179 struct Second { 180 @State message: string = 'Hi there'; 181 182 build() { 183 Row() { 184 Column() { 185 Text(this.message) 186 .fontSize(50) 187 .fontWeight(FontWeight.Bold) 188 Button() { 189 Text('Back') 190 .fontSize(25) 191 .fontWeight(FontWeight.Bold) 192 } 193 .type(ButtonType.Capsule) 194 .margin({ 195 top: 20 196 }) 197 .backgroundColor('#0D9FFB') 198 .width('40%') 199 .height('5%') 200 } 201 .width('100%') 202 } 203 .height('100%') 204 } 205 } 206 ``` 207 208 209## Implementing Page Redirection 210 211You 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. 212 213To deliver better transition effects, use [Navigation](../ui/arkts-navigation-navigation.md). 214 2151. Implement redirection from the first page to the second page. 216 217 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: 218 219 ```ts 220 // Index.ets 221 // Import the router module. 222 import { BusinessError } from '@kit.BasicServicesKit'; 223 224 @Entry 225 @Component 226 struct Index { 227 @State message: string = 'Hello World'; 228 229 build() { 230 Row() { 231 Column() { 232 Text(this.message) 233 .fontSize(50) 234 .fontWeight(FontWeight.Bold) 235 // Add a button to respond to user clicks. 236 Button() { 237 Text('Next') 238 .fontSize(30) 239 .fontWeight(FontWeight.Bold) 240 } 241 .type(ButtonType.Capsule) 242 .margin({ 243 top: 20 244 }) 245 .backgroundColor('#0D9FFB') 246 .width('40%') 247 .height('5%') 248 // Bind the onClick event to the Next button so that clicking the button redirects the user to the second page. 249 .onClick(() => { 250 console.info(`Succeeded in clicking the 'Next' button.`) 251 // Obtain UIContext. 252 let uiContext : UIContext = this.getUIContext(); 253 let router = uiContext.getRouter(); 254 // Go to the second page. 255 router.pushUrl({ url: 'pages/Second' }).then(() => { 256 console.info('Succeeded in jumping to the second page.') 257 }).catch((err: BusinessError) => { 258 console.error(`Failed to jump to the second page.Code is ${err.code}, message is ${err.message}`) 259 }) 260 }) 261 } 262 .width('100%') 263 } 264 .height('100%') 265 } 266 } 267 ``` 268 2692. Implement redirection from the second page to the first page. 270 271 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: 272 273 ```ts 274 // Second.ets 275 // Import the router module. 276 import { BusinessError } from '@kit.BasicServicesKit'; 277 278 @Entry 279 @Component 280 struct Second { 281 @State message: string = 'Hi there'; 282 283 build() { 284 Row() { 285 Column() { 286 Text(this.message) 287 .fontSize(50) 288 .fontWeight(FontWeight.Bold) 289 Button() { 290 Text('Back') 291 .fontSize(25) 292 .fontWeight(FontWeight.Bold) 293 } 294 .type(ButtonType.Capsule) 295 .margin({ 296 top: 20 297 }) 298 .backgroundColor('#0D9FFB') 299 .width('40%') 300 .height('5%') 301 // Bind the onClick event to the Back button so that clicking the button redirects the user back to the first page. 302 .onClick(() => { 303 console.info(`Succeeded in clicking the 'Back' button.`) 304 // Obtain UIContext. 305 let uiContext : UIContext = this.getUIContext(); 306 let router = uiContext.getRouter(); 307 try { 308 // Return to the first page. 309 router.back() 310 console.info('Succeeded in returning to the first page.') 311 } catch (err) { 312 let code = (err as BusinessError).code; 313 let message = (err as BusinessError).message; 314 console.error(`Failed to return to the first page.Code is ${code}, message is ${message}`) 315 } 316 }) 317 } 318 .width('100%') 319 } 320 .height('100%') 321 } 322 } 323 ``` 324 3253. Open the **Index.ets** file and click  in the Previewer to refresh the file. The display effect is shown in the figure below. 326 327  328 329 330## Running the Application on a Real Device 331 3321. Connect the development board running the OpenHarmony standard system to the computer. 333 3342. 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. 335 336  337 3383. On the toolbar in the upper right corner of the editing window, click . The display effect is shown in the figure below. 339 340  341 342Congratulations! You have finished developing your OpenHarmony application in ArkTS in the stage model. 343