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