1# 构建第一个ArkTS应用(Stage模型) 2 3 4> **说明:** 5> 6> 为确保运行效果,本文以使用**DevEco Studio 4.1 Beta1**版本为例<!--Del-->,点击[此处](../../release-notes/OpenHarmony-v4.1-beta1.md#配套关系)获取下载链接<!--DelEnd-->。 7 8## 创建ArkTS工程 9 101. 若首次打开**DevEco Studio**,请点击**Create Project**创建工程。如果已经打开了一个工程,请在菜单栏选择**File** > **New** > **Create Project**来创建一个新工程。 11 122. 选择**Application**应用开发(本文以应用开发为例,Atomic Service对应为原子化服务开发),选择模板“**[OpenHarmony]Empty Ability**”,点击**Next**进行下一步配置。 13 14  15 163. 进入配置工程界面,**Compile SDK**选择“**11**”,其他参数保持默认设置即可。 17 18 其中**Node**用来配置当前工程运行的Node.js版本,可选择使用已有的Node.js或下载新的Node.js版本。 19 20  21 224. 点击**Finish**,工具会自动生成示例代码和相关资源,等待工程创建完成。 23 24 25## ArkTS工程目录结构(Stage模型) 26 27 28 29- **AppScope > app.json5**:应用的全局配置信息,详见[app.json5配置文件](app-configuration-file.md)。 30 31- **entry**:OpenHarmony工程模块,编译构建生成一个HAP包。 32 - **src > main > ets**:用于存放ArkTS源码。 33 34 - **src > main > ets > entryability**:应用/服务的入口。 35 36 - **src > main > ets > pages**:应用/服务包含的页面。 37 38 - **src > main > resources**:用于存放应用/服务所用到的资源文件,如图形、多媒体、字符串、布局文件等。关于资源文件,详见[资源文件的分类](resource-categories-and-access.md#资源分类)。 39 40 - **src > main > module.json5**:模块配置文件。主要包含HAP包的配置信息、应用/服务在具体设备上的配置信息以及应用/服务的全局配置信息。具体的配置文件说明,详见[module.json5配置文件](module-configuration-file.md)。 41 42 - **build-profile.json5**:当前的模块信息 、编译信息配置项,包括buildOption、targets配置等。 43 44 - **hvigorfile.ts**:模块级编译构建任务脚本,开发者可以自定义相关任务和代码实现。 45 - **obfuscation-rules.txt**:混淆规则文件。混淆开启后,在使用Release模式进行编译时,会对代码进行编译、混淆及压缩处理,保护代码资产。 46 47- **oh_modules**:用于存放三方库依赖信息。 48 49- **build-profile.json5**:应用级配置信息,包括签名signingConfigs、产品配置products等。 50 51- **hvigorfile.ts**:应用级编译构建任务脚本。 52 53 54## 构建第一个页面 55 561. 使用文本组件。 57 58 工程同步完成后,在“**Project**”窗口,点击“**entry > src > main > ets > pages**”,打开“**Index.ets**”文件,可以看到页面由Text组件组成。“**Index.ets**”文件的示例如下: 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. 添加按钮。 82 83 在默认页面基础上,我们添加一个Button组件,作为按钮响应用户点击,从而实现跳转到另一个页面。“**Index.ets**”文件的示例如下: 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 // 添加按钮,以响应用户点击 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. 在编辑窗口右上角的侧边工具栏,点击Previewer,打开预览器。第一个页面效果如下图所示: 120 121  122 123 124## 构建第二个页面 125 1261. 创建第二个页面。 127 128 - 新建第二个页面文件。在“**Project**”窗口,打开“**entry > src > main > ets**”,右键点击“**pages**”文件夹,选择“**New > ArkTS File**”,命名为“**Second**”,点击**回车键**。可以看到文件目录结构如下: 129 130  131 132 > **说明:** 133 > 134 > 开发者也可以在右键点击“**pages**”文件夹时,选择“**New > Page > Empty Page**”,命名为“**Second**”,点击“**Finish**”完成第二个页面的创建。使用此种方式则无需再进行下文中第二个页面路由的手动配置。 135 136 - 配置第二个页面的路由。在“**Project**”窗口,打开“**entry > src > main > resources > base > profile**”,在main_pages.json文件中的“src”下配置第二个页面的路由“pages/Second”。示例如下: 137 138 ```json 139 { 140 "src": [ 141 "pages/Index", 142 "pages/Second" 143 ] 144 } 145 ``` 146 1472. 添加文本及按钮。 148 149 参照第一个页面,在第二个页面添加Text组件、Button组件等,并设置其样式。“**Second.ets**”文件的示例如下: 150 151 ```ts 152 // Second.ets 153 @Entry 154 @Component 155 struct Second { 156 @State message: string = 'Hi there'; 157 158 build() { 159 Row() { 160 Column() { 161 Text(this.message) 162 .fontSize(50) 163 .fontWeight(FontWeight.Bold) 164 Button() { 165 Text('Back') 166 .fontSize(25) 167 .fontWeight(FontWeight.Bold) 168 } 169 .type(ButtonType.Capsule) 170 .margin({ 171 top: 20 172 }) 173 .backgroundColor('#0D9FFB') 174 .width('40%') 175 .height('5%') 176 } 177 .width('100%') 178 } 179 .height('100%') 180 } 181 } 182 ``` 183 184 185## 实现页面间的跳转 186 187页面间的导航可以通过[页面路由router](../reference/apis-arkui/js-apis-router.md)来实现。页面路由router根据页面url找到目标页面,从而实现跳转。使用页面路由请导入router模块。 188 189如果需要实现更好的转场动效等,推荐使用[Navigation](../ui/arkts-navigation-navigation.md)。 190 1911. 第一个页面跳转到第二个页面。 192 193 在第一个页面中,跳转按钮绑定onClick事件,点击按钮时跳转到第二页。“**Index.ets**”文件的示例如下: 194 195 ```ts 196 // Index.ets 197 // 导入页面路由模块 198 import { router } from '@kit.ArkUI'; 199 import { BusinessError } from '@kit.BasicServicesKit'; 200 201 @Entry 202 @Component 203 struct Index { 204 @State message: string = 'Hello World'; 205 206 build() { 207 Row() { 208 Column() { 209 Text(this.message) 210 .fontSize(50) 211 .fontWeight(FontWeight.Bold) 212 // 添加按钮,以响应用户点击 213 Button() { 214 Text('Next') 215 .fontSize(30) 216 .fontWeight(FontWeight.Bold) 217 } 218 .type(ButtonType.Capsule) 219 .margin({ 220 top: 20 221 }) 222 .backgroundColor('#0D9FFB') 223 .width('40%') 224 .height('5%') 225 // 跳转按钮绑定onClick事件,点击时跳转到第二页 226 .onClick(() => { 227 console.info(`Succeeded in clicking the 'Next' button.`) 228 // 跳转到第二页 229 router.pushUrl({ url: 'pages/Second' }).then(() => { 230 console.info('Succeeded in jumping to the second page.') 231 }).catch((err: BusinessError) => { 232 console.error(`Failed to jump to the second page.Code is ${err.code}, message is ${err.message}`) 233 }) 234 }) 235 } 236 .width('100%') 237 } 238 .height('100%') 239 } 240 } 241 ``` 242 2432. 第二个页面返回到第一个页面。 244 245 在第二个页面中,返回按钮绑定onClick事件,点击按钮时返回到第一页。“**Second.ets**”文件的示例如下: 246 247 ```ts 248 // Second.ets 249 // 导入页面路由模块 250 import { router } from '@kit.ArkUI'; 251 import { BusinessError } from '@kit.BasicServicesKit'; 252 253 @Entry 254 @Component 255 struct Second { 256 @State message: string = 'Hi there'; 257 258 build() { 259 Row() { 260 Column() { 261 Text(this.message) 262 .fontSize(50) 263 .fontWeight(FontWeight.Bold) 264 Button() { 265 Text('Back') 266 .fontSize(25) 267 .fontWeight(FontWeight.Bold) 268 } 269 .type(ButtonType.Capsule) 270 .margin({ 271 top: 20 272 }) 273 .backgroundColor('#0D9FFB') 274 .width('40%') 275 .height('5%') 276 // 返回按钮绑定onClick事件,点击按钮时返回到第一页 277 .onClick(() => { 278 console.info(`Succeeded in clicking the 'Back' button.`) 279 try { 280 // 返回第一页 281 router.back() 282 console.info('Succeeded in returning to the first page.') 283 } catch (err) { 284 let code = (err as BusinessError).code; 285 let message = (err as BusinessError).message; 286 console.error(`Failed to return to the first page.Code is ${code}, message is ${message}`) 287 } 288 }) 289 } 290 .width('100%') 291 } 292 .height('100%') 293 } 294 } 295 ``` 296 2973. 打开Index.ets文件,点击预览器中的按钮进行刷新。效果如下图所示: 298 299  300 301 302## 使用真机运行应用 303 3041. 将搭载OpenHarmony标准系统的开发板与电脑连接。 305 3062. 点击**File** > **Project Structure...** > **Project** > **SigningConfigs**界面勾选“**Automatically generate signature**”,等待自动签名完成即可,点击“**OK**”。如下图所示: 307 308  309 3103. 在编辑窗口右上角的工具栏,点击按钮运行。效果如下图所示: 311 312  313 314恭喜您已经使用ArkTS语言开发(Stage模型)完成了第一个OpenHarmony应用,快来探索更多的OpenHarmony功能吧。