• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# 构建第一个ArkTS应用(Stage模型)
2
3
4> **说明:**
5>
6> 为确保运行效果,本文以使用**DevEco Studio 4.1 Beta1**版本为例,点击[此处](../../release-notes/OpenHarmony-v4.1-beta1.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   ![createProject](figures/createProject.png)
15
163. 进入配置工程界面,**Compile SDK**选择“**11**”,其他参数保持默认设置即可。
17
18   其中**Node**用来配置当前工程运行的Node.js版本,可选择使用已有的Node.js或下载新的Node.js版本。
19
20   ![chooseStageModel](figures/chooseStageModel.png)
21
224. 点击**Finish**,工具会自动生成示例代码和相关资源,等待工程创建完成。
23
24
25## ArkTS工程目录结构(Stage模型)
26
27![project](figures/project.png)
28
29- **AppScope > app.json5**:应用的全局配置信息。
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   ![zh-cn_image_0000001311334976](figures/zh-cn_image_0000001311334976.png)
122
123
124## 构建第二个页面
125
1261. 创建第二个页面。
127
128   - 新建第二个页面文件。在“**Project**”窗口,打开“**entry > src > main > ets**”,右键点击“**pages**”文件夹,选择“**New > ArkTS File**”,命名为“**Second**”,点击“**Finish**”。可以看到文件目录结构如下:
129
130      ![secondPage](figures/secondPage.png)
131
132      >  **说明:**
133      >
134      > 开发者也可以在右键点击“**pages**”文件夹时,选择“**New > Page**”,则无需手动配置相关页面路由。
135   - 配置第二个页面的路由。在“**Project**”窗口,打开“**entry > src > main > resources > base > profile**”,在main_pages.json文件中的“src”下配置第二个页面的路由“pages/Second”。示例如下:
136
137      ```json
138      {
139        "src": [
140          "pages/Index",
141          "pages/Second"
142        ]
143      }
144      ```
145
1462. 添加文本及按钮。
147
148   参照第一个页面,在第二个页面添加Text组件、Button组件等,并设置其样式。“**Second.ets**”文件的示例如下:
149
150   ```ts
151   // Second.ets
152   @Entry
153   @Component
154   struct Second {
155     @State message: string = 'Hi there';
156
157     build() {
158       Row() {
159         Column() {
160           Text(this.message)
161             .fontSize(50)
162             .fontWeight(FontWeight.Bold)
163           Button() {
164             Text('Back')
165               .fontSize(25)
166               .fontWeight(FontWeight.Bold)
167           }
168           .type(ButtonType.Capsule)
169           .margin({
170             top: 20
171           })
172           .backgroundColor('#0D9FFB')
173           .width('40%')
174           .height('5%')
175         }
176         .width('100%')
177       }
178       .height('100%')
179     }
180   }
181   ```
182
183
184## 实现页面间的跳转
185
186页面间的导航可以通过[页面路由router](../reference/apis-arkui/js-apis-router.md)来实现。页面路由router根据页面url找到目标页面,从而实现跳转。使用页面路由请导入router模块。
187
188如果需要实现更好的转场动效等,推荐使用[Navigation](../ui/arkts-navigation-navigation.md)。
189
1901. 第一个页面跳转到第二个页面。
191
192   在第一个页面中,跳转按钮绑定onClick事件,点击按钮时跳转到第二页。“**Index.ets**”文件的示例如下:
193
194   ```ts
195   // Index.ets
196   // 导入页面路由模块
197   import router from '@ohos.router';
198   import { BusinessError } from '@ohos.base';
199
200   @Entry
201   @Component
202   struct Index {
203     @State message: string = 'Hello World';
204
205     build() {
206       Row() {
207         Column() {
208           Text(this.message)
209             .fontSize(50)
210             .fontWeight(FontWeight.Bold)
211           // 添加按钮,以响应用户点击
212           Button() {
213             Text('Next')
214               .fontSize(30)
215               .fontWeight(FontWeight.Bold)
216           }
217           .type(ButtonType.Capsule)
218           .margin({
219             top: 20
220           })
221           .backgroundColor('#0D9FFB')
222           .width('40%')
223           .height('5%')
224           // 跳转按钮绑定onClick事件,点击时跳转到第二页
225           .onClick(() => {
226             console.info(`Succeeded in clicking the 'Next' button.`)
227            // 跳转到第二页
228              router.pushUrl({ url: 'pages/Second' }).then(() => {
229                console.info('Succeeded in jumping to the second page.')
230              }).catch((err: BusinessError) => {
231                console.error(`Failed to jump to the second page.Code is ${err.code}, message is ${err.message}`)
232              })
233           })
234         }
235         .width('100%')
236       }
237       .height('100%')
238     }
239   }
240   ```
241
2422. 第二个页面返回到第一个页面。
243
244   在第二个页面中,返回按钮绑定onClick事件,点击按钮时返回到第一页。“**Second.ets**”文件的示例如下:
245
246   ```ts
247   // Second.ets
248   // 导入页面路由模块
249   import router from '@ohos.router';
250   import { BusinessError } from '@ohos.base';
251
252   @Entry
253   @Component
254   struct Second {
255     @State message: string = 'Hi there';
256
257     build() {
258       Row() {
259         Column() {
260           Text(this.message)
261             .fontSize(50)
262             .fontWeight(FontWeight.Bold)
263           Button() {
264             Text('Back')
265               .fontSize(25)
266               .fontWeight(FontWeight.Bold)
267           }
268           .type(ButtonType.Capsule)
269           .margin({
270             top: 20
271           })
272           .backgroundColor('#0D9FFB')
273           .width('40%')
274           .height('5%')
275           // 返回按钮绑定onClick事件,点击按钮时返回到第一页
276           .onClick(() => {
277             console.info(`Succeeded in clicking the 'Back' button.`)
278             try {
279               // 返回第一页
280               router.back()
281               console.info('Succeeded in returning to the first page.')
282             } catch (err) {
283                let code = (err as BusinessError).code;
284                let message = (err as BusinessError).message;
285               console.error(`Failed to return to the first page.Code is ${code}, message is ${message}`)
286             }
287           })
288         }
289         .width('100%')
290       }
291       .height('100%')
292     }
293   }
294   ```
295
2963. 打开Index.ets文件,点击预览器中的![zh-cn_image_0000001311015192](figures/zh-cn_image_0000001311015192.png)按钮进行刷新。效果如下图所示:
297
298   ![zh-cn_image_0000001364254729](figures/zh-cn_image_0000001364254729.png)
299
300
301## 使用真机运行应用
302
3031. 将搭载OpenHarmony标准系统的开发板与电脑连接。
304
3052. 点击**File** > **Project Structure...** > **Project** > **SigningConfigs**界面勾选“**Automatically generate signature**”,等待自动签名完成即可,点击“**OK**”。如下图所示:
306
307   ![signConfig](figures/signConfig.png)
308
3093. 在编辑窗口右上角的工具栏,点击![zh-cn_image_0000001364054485](figures/zh-cn_image_0000001364054485.png)按钮运行。效果如下图所示:
310
311   ![zh-cn_image_0000001364254729](figures/zh-cn_image_0000001364254729.png)
312
313恭喜您已经使用ArkTS语言开发(Stage模型)完成了第一个OpenHarmony应用,快来[探索更多的OpenHarmony功能](../application-dev-guide.md)吧。