• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# 使用ArkTS语言开发(Stage模型)
2
3
4> **说明:**
5>
6> 请使用**DevEco Studio V3.0.0.900 Beta3**及更高版本。
7>
8> 为确保运行效果,本文以使用**DevEco Studio V3.1.0.100**版本为例,点击[此处](https://developer.harmonyos.com/cn/develop/deveco-studio)获取下载链接。
9
10
11## 创建ArkTS工程
12
131. 若首次打开**DevEco Studio**,请点击**Create Project**创建工程。如果已经打开了一个工程,请在菜单栏选择**File** > **New** > **Create Project**来创建一个新工程。选择**OpenHarmony**模板库,选择模板“**Empty Ability**”,点击**Next**进行下一步配置。
14
15   ![01](figures/01.png)
16
172. 进入配置工程界面,**Compile SDK**选择“**9**”,**Model** 选择“**Stage**”,其他参数保持默认设置即可。
18
19   ![07](figures/07.png)
20
21   > **说明:**
22   >
23   > 支持使用ArkTS[低代码开发](https://developer.harmonyos.com/cn/docs/documentation/doc-guides-V3/ohos-ide-low-code-overview-0000001445605884-V3)方式。
24   >
25   > 低代码开发方式具有丰富的UI界面编辑功能,通过可视化界面开发方式快速构建布局,可有效降低开发者的上手成本并提升开发者构建UI界面的效率。
26   >
27   > 如需使用低代码开发方式,请打开上图中的Enable Super Visual开关。
28
293. 点击**Finish**,工具会自动生成示例代码和相关资源,等待工程创建完成。
30
31
32## ArkTS工程目录结构(Stage模型)
33
34![zh-cn_image_0000001364054489](figures/zh-cn_image_0000001364054489.png)
35
36- **entry**:OpenHarmony工程模块,编译构建生成一个[HAP](../../glossary.md#hap)包。
37  - **src > main > ets**:用于存放ets源码。
38  - **src > main > ets > entryability**:应用/服务的入口。
39  - **src > main > ets > pages**:应用/服务包含的页面。
40  - **src > main > resources**:用于存放应用/服务所用到的资源文件,如图形、多媒体、字符串、布局文件等。关于资源文件,详见[资源文件的分类](resource-categories-and-access.md#资源分类)。
41  - **src > main > module.json5**:模块配置文件。主要包含HAP包的配置信息、应用/服务在具体设备上的配置信息以及应用/服务的全局配置信息。具体的配置文件说明,详见[module.json5配置文件](module-configuration-file.md)。
42  - **build-profile.json5**:当前的模块信息 、编译信息配置项,包括buildOption、targets配置等。
43  - **hvigorfile.ts**:模块级编译构建任务脚本,开发者可以自定义相关任务和代码实现。
44
45- **build-profile.json5**:应用级配置信息,包括签名、产品配置等。
46
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   ![zh-cn_image_0000001311334976](figures/zh-cn_image_0000001311334976.png)
118
119
120## 构建第二个页面
121
1221. 创建第二个页面。
123
124   - 新建第二个页面文件。在“**Project**”窗口,打开“**entry > src > main > ets**”,右键点击“**pages**”文件夹,选择“**New > ArkTS File**”,命名为“**Second**”,点击“**Finish**”。可以看到文件目录结构如下:
125
126      ![09](figures/09.png)
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             router.pushUrl({ url: 'pages/Second' })
220           })
221         }
222         .width('100%')
223       }
224       .height('100%')
225     }
226   }
227   ```
228
2292. 第二个页面返回到第一个页面。
230
231   在第二个页面中,返回按钮绑定onClick事件,点击按钮时返回到第一页。“**Second.ets**”文件的示例如下:
232
233   ```ts
234   // Second.ets
235   // 导入页面路由模块
236   import router from '@ohos.router';
237
238   @Entry
239   @Component
240   struct Second {
241     @State message: string = 'Hi there'
242
243     build() {
244       Row() {
245         Column() {
246           Text(this.message)
247             .fontSize(50)
248             .fontWeight(FontWeight.Bold)
249           Button() {
250             Text('Back')
251               .fontSize(25)
252               .fontWeight(FontWeight.Bold)
253           }
254           .type(ButtonType.Capsule)
255           .margin({
256             top: 20
257           })
258           .backgroundColor('#0D9FFB')
259           .width('40%')
260           .height('5%')
261           // 返回按钮绑定onClick事件,点击按钮时返回到第一页
262           .onClick(() => {
263             router.back()
264           })
265         }
266         .width('100%')
267       }
268       .height('100%')
269     }
270   }
271   ```
272
2733. 打开Index.ets文件,点击预览器中的![zh-cn_image_0000001311015192](figures/zh-cn_image_0000001311015192.png)按钮进行刷新。效果如下图所示:
274
275   ![zh-cn_image_0000001364254729](figures/zh-cn_image_0000001364254729.png)
276
277
278## 使用真机运行应用
279
2801. 将搭载OpenHarmony标准系统的开发板与电脑连接。
281
2822. 点击**File** > **Project Structure...** > **Project** > **SigningConfigs**界面勾选“**Automatically generate signature**”,等待自动签名完成即可,点击“**OK**”。如下图所示:
283
284   ![06](figures/06.png)
285
2863. 在编辑窗口右上角的工具栏,点击![zh-cn_image_0000001364054485](figures/zh-cn_image_0000001364054485.png)按钮运行。效果如下图所示:
287
288   ![zh-cn_image_0000001364254729](figures/zh-cn_image_0000001364254729.png)
289
290恭喜您已经使用ArkTS语言开发(Stage模型)完成了第一个OpenHarmony应用,快来[探索更多的OpenHarmony功能](../application-dev-guide.md)吧。
291