1# 构建第一个ArkTS应用(Stage模型) 2 3## 创建ArkTS工程 4 5> **说明:** 6> 7> - 4.1Beta1版本及之后,DevEco Studio默认创建的工程为HarmonyOS工程,不再支持直接创建OpenHarmony工程。需要基于创建完成的HarmonyOS工程进行一些字段修改,才能得到OpenHarmony工程。 8> 9> - 为确保运行效果,此处以使用[最新DevEco Studio版本](https://developer.huawei.com/consumer/cn/download/)为例。 10 111. 若首次打开**DevEco Studio**,请点击**Create Project**创建工程。如果已经打开了一个工程,请在菜单栏选择**File > New > Create Project**来创建一个新工程。 12 132. 选择**Application**应用开发(本文以应用开发为例,**Atomic Service**对应为元服务开发),选择模板**Empty Ability**,点击**Next**进行下一步配置。 14 15 若开发者需要进行Native相关工程的开发,请选择Native C++模板,更多模板的使用和说明请见[工程模板介绍](https://developer.huawei.com/consumer/cn/doc/harmonyos-guides/ide-template)。 16 17  18 193. 进入配置工程界面,**Compatible SDK**表示兼容的最低API Version,此处以选择5.0.0(12)为例,其他参数保持默认设置即可。 20 21  22 234. 点击**Finish**,工具会自动生成示例代码和相关资源,等待工程创建完成,此时创建的工程为HarmonyOS工程。 24 255. 在完成创建HarmonyOS工程后,根据如下操作修改工程级build-profile.json5文件(即与entry同级)中相关字段: 26 27 1. 在工程级build-profile.json5文件添加compileSdkVersion字段。 28 29 2. 将compatibleSdkVersion和compileSdkVersion字段赋值为整数类型,如10,11或12。 30 31 3. 将runtimeOS从"HarmonyOS"修改为"OpenHarmony"。 32 33 ```json 34 "products": [ 35 { 36 "name": "default", 37 "signingConfig": "default", 38 "compileSdkVersion": 12, // 指定OpenHarmony应用/原子化服务编译时的版本 39 "compatibleSdkVersion": 12, // 指定OpenHarmony应用/原子化服务兼容的最低版本 40 "runtimeOS": "OpenHarmony", 41 } 42 ], 43 ``` 44 456. 单击**Sync Now**进行同步。 46 47 在Sync Check弹窗中点击Yes,同意将module.json5/config.json文件中的phone切换为OpenHarmony支持的default类型,并删除在OpenHarmony不适用的其他设备类型,同步成功无其他报错则OpenHarmony工程创建完成。 48 49## ArkTS工程目录结构(Stage模型) 50 51 52 53- **AppScope > app.json5**:应用的全局配置信息,详见[app.json5配置文件](app-configuration-file.md)。 54 55- **entry**:应用/原子化服务模块,编译构建生成一个HAP包。 56 - **src > main > ets**:用于存放ArkTS源码。 57 58 - **src > main > ets > entryability**:应用/服务的入口。 59 60 - **src > main > ets > pages**:应用/服务包含的页面。 61 62 - **src > main > resources**:用于存放应用/服务所用到的资源文件,如图形、多媒体、字符串、布局文件等。关于资源文件,详见[资源文件的分类](resource-categories-and-access.md#资源分类)。 63 64 - **src > main > module.json5**:模块配置文件。主要包含HAP包的配置信息、应用/服务在具体设备上的配置信息以及应用/服务的全局配置信息。具体的配置文件说明,详见[module.json5配置文件](module-configuration-file.md)。 65 66 - **build-profile.json5**:当前的模块信息 、编译信息配置项,包括buildOption、targets配置等。 67 68 - **hvigorfile.ts**:模块级编译构建任务脚本,开发者可以自定义相关任务和代码实现。 69 - **obfuscation-rules.txt**:混淆规则文件。混淆开启后,在使用Release模式进行编译时,会对代码进行编译、混淆及压缩处理,保护代码资产。 70 71- **oh_modules**:用于存放三方库依赖信息。 72 73- **build-profile.json5**:工程级配置信息,包括签名signingConfigs、产品配置products等。 74 75- **hvigorfile.ts**:应用级编译构建任务脚本。 76 77 78## 构建第一个页面 79 801. 使用文本组件。 81 82 工程同步完成后,在Project窗口,点击entry > src > main > ets > pages,打开Index.ets文件,可以看到页面由Text组件组成。**Index.ets**文件的示例如下: 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. 添加按钮。 106 107 在默认页面基础上,我们添加一个Button组件,作为按钮响应用户点击,从而实现跳转到另一个页面。**Index.ets**文件的示例如下: 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 // 添加按钮,以响应用户点击 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. 在编辑窗口右上角的侧边工具栏,点击Previewer,打开预览器。第一个页面效果如下图所示: 144 145  146 147 148## 构建第二个页面 149 1501. 创建第二个页面。 151 152 - 新建第二个页面文件。在“**Project**”窗口,打开“**entry > src > main > ets**”,右键点击“**pages**”文件夹,选择“**New > ArkTS File**”,命名为“**Second**”,点击**回车键**。可以看到文件目录结构如下: 153 154  155 156 > **说明:** 157 > 158 > 开发者也可以在右键点击“**pages**”文件夹时,选择“**New > Page > Empty Page**”,命名为“**Second**”,点击“**Finish**”完成第二个页面的创建。使用此种方式则无需再进行下文中第二个页面路由的手动配置。 159 160 - 配置第二个页面的路由。在“**Project**”窗口,打开“**entry > src > main > resources > base > profile**”,在main_pages.json文件中的“src”下配置第二个页面的路由“pages/Second”。示例如下: 161 162 ```json 163 { 164 "src": [ 165 "pages/Index", 166 "pages/Second" 167 ] 168 } 169 ``` 170 1712. 添加文本及按钮。 172 173 参照第一个页面,在第二个页面添加Text组件、Button组件等,并设置其样式。“**Second.ets**”文件的示例如下: 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## 实现页面间的跳转 210 211页面间的导航可以通过[页面路由router](../reference/apis-arkui/js-apis-router.md)来实现。页面路由router根据页面url找到目标页面,从而实现跳转。使用页面路由请导入router模块。 212 213如果需要实现更好的转场动效等,推荐使用[Navigation](../ui/arkts-navigation-navigation.md)。 214 2151. 第一个页面跳转到第二个页面。 216 217 在第一个页面中,跳转按钮绑定onClick事件,点击按钮时跳转到第二页。“**Index.ets**”文件的示例如下: 218 219 ```ts 220 // Index.ets 221 // 导入页面路由模块 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 // 添加按钮,以响应用户点击 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 // 跳转按钮绑定onClick事件,点击时跳转到第二页 249 .onClick(() => { 250 console.info(`Succeeded in clicking the 'Next' button.`) 251 // 获取UIContext 252 let uiContext : UIContext = this.getUIContext(); 253 let router = uiContext.getRouter(); 254 // 跳转到第二页 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. 第二个页面返回到第一个页面。 270 271 在第二个页面中,返回按钮绑定onClick事件,点击按钮时返回到第一页。“**Second.ets**”文件的示例如下: 272 273 ```ts 274 // Second.ets 275 // 导入页面路由模块 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 // 返回按钮绑定onClick事件,点击按钮时返回到第一页 302 .onClick(() => { 303 console.info(`Succeeded in clicking the 'Back' button.`) 304 // 获取UIContext 305 let uiContext : UIContext = this.getUIContext(); 306 let router = uiContext.getRouter(); 307 try { 308 // 返回第一页 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. 打开Index.ets文件,点击预览器中的按钮进行刷新。效果如下图所示: 326 327  328 329 330## 使用真机运行应用 331 3321. 将搭载OpenHarmony标准系统的开发板与电脑连接。 333 3342. 点击**File** > **Project Structure...** > **Project** > **SigningConfigs**界面勾选“**Automatically generate signature**”,等待自动签名完成即可,点击“**OK**”。如下图所示: 335 336  337 3383. 在编辑窗口右上角的工具栏,点击按钮运行。效果如下图所示: 339 340  341 342恭喜您已经使用ArkTS语言开发(Stage模型)完成了第一个OpenHarmony应用,快来探索更多的OpenHarmony功能吧。