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