start-with-ets-stage.md 9.8 KB
Newer Older
1
# 使用ArkTS语言开发(Stage模型)
Z
zengyawen 已提交
2

G
ge-yafang 已提交
3 4 5

> **说明:**
> 请使用**DevEco Studio V3.0.0.900 Beta3**及更高版本。
Z
zengyawen 已提交
6
> 
G
ge-yafang 已提交
7
> 为确保运行效果,本文以使用**DevEco Studio V3.0.0.993**版本为例,点击[此处](https://developer.harmonyos.com/cn/develop/deveco-studio#download)获取下载链接。
Z
zengyawen 已提交
8 9 10 11


## 创建eTS工程

G
ge-yafang 已提交
12
1. 若首次打开**DevEco Studio**,请点击**Create Project**创建工程。如果已经打开了一个工程,请在菜单栏选择**File** > **New** > **Create Project**来创建一个新工程。选择**OpenHarmony**模板库,选择模板“**Empty Ability**”,点击**Next**进行下一步配置。
13

G
ge-yafang 已提交
14
   ![01](figures/01.png)
15

G
ge-yafang 已提交
16
2. 进入配置工程界面,**Compile SDK**选择“**9**”,**Model** 选择“**Stage**”,其他参数保持默认设置即可。
17

G
ge-yafang 已提交
18
   ![07](figures/07.png)
Z
zengyawen 已提交
19

G
ge-yafang 已提交
20 21
   > **说明:**
   >
22
   > 支持使用ArkTS[低代码开发](https://developer.harmonyos.com/cn/docs/documentation/doc-guides/ohos-low-code-development-0000001218440652)方式。
G
ge-yafang 已提交
23 24 25 26 27 28
   >
   > 低代码开发方式具有丰富的UI界面编辑功能,通过可视化界面开发方式快速构建布局,可有效降低开发者的上手成本并提升开发者构建UI界面的效率。
   >
   > 如需使用低代码开发方式,请打开上图中的Enable Super Visual开关。
   
3. 点击**Finish**,工具会自动生成示例代码和相关资源,等待工程创建完成。
29

Z
zengyawen 已提交
30

G
ge-yafang 已提交
31
## eTS工程目录结构
Z
zengyawen 已提交
32

G
ge-yafang 已提交
33
![zh-cn_image_0000001364054489](figures/zh-cn_image_0000001364054489.png)
Z
zengyawen 已提交
34

G
ge-yafang 已提交
35
- **AppScope > app.json5**:应用的全局配置信息。
Z
zengyawen 已提交
36

G
ge-yafang 已提交
37 38 39 40 41 42
- **entry**:OpenHarmony工程模块,编译构建生成一个[HAP](../../glossary.md#hap)包。
  - **src > main > ets**:用于存放ets源码。
  - **src > main > ets > Application > AbilityStage.ts**:实现AbilityStage接口。
  - **src > main > ets > MainAbility**:应用/服务的入口。
  - **src > main > ets > MainAbility > MainAbility.ts**:承载Ability生命周期。
  - **src > main > ets > pages**:MainAbility包含的页面。
43
  - **src > main > resources**:用于存放应用/服务所用到的资源文件,如图形、多媒体、字符串、布局文件等。关于资源文件,详见[资源文件的分类](resource-categories-and-access.md#资源分类)
G
ge-yafang 已提交
44 45 46
  - **src > main > module.json5**:模块配置文件。主要包含HAP包的配置信息、应用/服务在具体设备上的配置信息以及应用/服务的全局配置信息。具体的配置文件说明,详见[应用包结构配置文件的说明(Stage模型)](stage-structure.md)
  - **build-profile.json5**:当前的模块信息 、编译信息配置项,包括buildOption、targets配置等。
  - **hvigorfile.js**:模块级编译构建任务脚本,开发者可以自定义相关任务和代码实现。
Z
zengyawen 已提交
47

G
ge-yafang 已提交
48
- **build-profile.json5**:应用级配置信息,包括签名、产品配置等。
Z
zengyawen 已提交
49

G
ge-yafang 已提交
50
- **hvigorfile.js**:应用级编译构建任务脚本。
Z
zengyawen 已提交
51 52 53 54


## 构建第一个页面

55
1. 使用文本组件。
56

57
   工程同步完成后,在“**Project**”窗口,点击“**entry > src > main > ets > pages**”,打开“**index.ets**”文件,可以看到页面由Text组件组成。“**index.ets**”文件的示例如下:
Z
zengyawen 已提交
58
   
59 60
   ```ts
   // index.ets
Z
zengyawen 已提交
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
   @Entry
   @Component
   struct Index {
     @State message: string = 'Hello World'
   
     build() {
       Row() {
         Column() {
           Text(this.message)
             .fontSize(50)
             .fontWeight(FontWeight.Bold)
         }
         .width('100%')
       }
       .height('100%')
     }
   }
   ```

80
2. 添加按钮。
81

G
ge-yafang 已提交
82
   在默认页面基础上,我们添加一个Button组件,作为按钮响应用户点击,从而实现跳转到另一个页面。“**index.ets**”文件的示例如下:
Z
zengyawen 已提交
83
   
84 85
   ```ts
   // index.ets
Z
zengyawen 已提交
86 87 88 89 90 91 92 93 94 95 96
   @Entry
   @Component
   struct Index {
     @State message: string = 'Hello World'
   
     build() {
       Row() {
         Column() {
           Text(this.message)
             .fontSize(50)
             .fontWeight(FontWeight.Bold)
G
ge-yafang 已提交
97
           // 添加按钮,以响应用户点击
Z
zengyawen 已提交
98
           Button() {
Z
zengyawen 已提交
99
             Text('Next')
Z
zengyawen 已提交
100 101 102 103
               .fontSize(30)
               .fontWeight(FontWeight.Bold)
           }
           .type(ButtonType.Capsule)
Z
zengyawen 已提交
104
           .margin({
Z
zengyawen 已提交
105
             top: 20
Z
zengyawen 已提交
106 107 108 109 110 111 112 113 114 115 116 117
           })
           .backgroundColor('#0D9FFB')
           .width('40%')
           .height('5%')
         }
         .width('100%')
       }
       .height('100%')
     }
   }
   ```

118
3. 在编辑窗口右上角的侧边工具栏,点击Previewer,打开预览器。第一个页面效果如下图所示:
119

G
ge-yafang 已提交
120
   ![zh-cn_image_0000001311334976](figures/zh-cn_image_0000001311334976.png)
Z
zengyawen 已提交
121 122 123 124


## 构建第二个页面

125
1. 创建第二个页面。
126

G
ge-yafang 已提交
127
   - 新建第二个页面文件。在“**Project**”窗口,打开“**entry > src > main > ets**”,右键点击“**pages**”文件夹,选择“**New > eTS File**”,命名为“**second**”,点击“**Finish**”。可以看到文件目录结构如下:
128

G
ge-yafang 已提交
129
      ![09](figures/09.png)
130

G
ge-yafang 已提交
131 132
      > **说明:**
      > 开发者也可以在右键点击“**pages**”文件夹时,选择“**New > Page**”,则无需手动配置相关页面路由。
133
   - 配置第二个页面的路由。在“**Project**”窗口,打开“**entry > src > main > resources > base > profile**”,在main_pages.json文件中的“src”下配置第二个页面的路由“pages/second”。示例如下:
G
ge-yafang 已提交
134 135
     
      ```json
136
      {
G
ge-yafang 已提交
137 138 139 140
        "src": [
          "pages/index",
          "pages/second"
        ]
141
      }
G
ge-yafang 已提交
142
      ```
Z
zengyawen 已提交
143

144
2. 添加文本及按钮。
145

Z
zengyawen 已提交
146
   参照第一个页面,在第二个页面添加Text组件、Button组件等,并设置其样式。“**second.ets**”文件的示例如下:
G
ge-yafang 已提交
147
   
148 149
   ```ts
   // second.ets
Z
zengyawen 已提交
150 151 152 153
   @Entry
   @Component
   struct Second {
     @State message: string = 'Hi there'
Z
zengyawen 已提交
154 155 156 157 158 159
   
     build() {
       Row() {
         Column() {
           Text(this.message)
             .fontSize(50)
Z
zengyawen 已提交
160
             .fontWeight(FontWeight.Bold)
Z
zengyawen 已提交
161 162 163 164 165 166 167 168 169 170 171 172 173 174
           Button() {
             Text('Back')
               .fontSize(25)
               .fontWeight(FontWeight.Bold)
           }
           .type(ButtonType.Capsule)
           .margin({
             top: 20
           })
           .backgroundColor('#0D9FFB')
           .width('40%')
           .height('5%')
         }
         .width('100%')
Z
zengyawen 已提交
175
       }
Z
zengyawen 已提交
176
       .height('100%')
Z
zengyawen 已提交
177 178 179 180 181 182 183
     }
   }
   ```


## 实现页面间的跳转

184
页面间的导航可以通过[页面路由router](../reference/apis/js-apis-router.md)来实现。页面路由router根据页面url找到目标页面,从而实现跳转。使用页面路由请导入router模块。
Z
zengyawen 已提交
185

186
1. 第一个页面跳转到第二个页面。
187

188
   在第一个页面中,跳转按钮绑定onClick事件,点击按钮时跳转到第二页。“**index.ets**”文件的示例如下:
Z
zengyawen 已提交
189
   
190 191
   ```ts
   // index.ets
192
   import router from '@ohos.router';
Z
zengyawen 已提交
193 194 195 196 197 198 199 200 201 202 203 204
   
   @Entry
   @Component
   struct Index {
     @State message: string = 'Hello World'
   
     build() {
       Row() {
         Column() {
           Text(this.message)
             .fontSize(50)
             .fontWeight(FontWeight.Bold)
G
ge-yafang 已提交
205
           // 添加按钮,以响应用户点击
Z
zengyawen 已提交
206
           Button() {
Z
zengyawen 已提交
207
             Text('Next')
Z
zengyawen 已提交
208 209 210 211
               .fontSize(30)
               .fontWeight(FontWeight.Bold)
           }
           .type(ButtonType.Capsule)
Z
zengyawen 已提交
212
           .margin({
Z
zengyawen 已提交
213
             top: 20
Z
zengyawen 已提交
214 215 216 217
           })
           .backgroundColor('#0D9FFB')
           .width('40%')
           .height('5%')
Z
zengyawen 已提交
218 219
           // 跳转按钮绑定onClick事件,点击时跳转到第二页
           .onClick(() => {
220
             router.push({ url: 'pages/second' })
Z
zengyawen 已提交
221 222 223 224 225 226 227 228 229
           })
         }
         .width('100%')
       }
       .height('100%')
     }
   }
   ```

230
2. 第二个页面返回到第一个页面。
231

232
   在第二个页面中,返回按钮绑定onClick事件,点击按钮时返回到第一页。“**second.ets**”文件的示例如下:
Z
zengyawen 已提交
233
   
234 235
   ```ts
   // second.ets
236
   import router from '@ohos.router';
Z
zengyawen 已提交
237 238 239 240
   
   @Entry
   @Component
   struct Second {
Z
zengyawen 已提交
241 242 243 244 245 246 247 248 249 250 251 252
     @State message: string = 'Hi there'
   
     build() {
       Row() {
         Column() {
           Text(this.message)
             .fontSize(50)
             .fontWeight(FontWeight.Bold)
           Button() {
             Text('Back')
               .fontSize(25)
               .fontWeight(FontWeight.Bold)
Z
zengyawen 已提交
253
           }
Z
zengyawen 已提交
254 255 256 257 258 259 260 261 262 263 264 265 266
           .type(ButtonType.Capsule)
           .margin({
             top: 20
           })
           .backgroundColor('#0D9FFB')
           .width('40%')
           .height('5%')
           // 返回按钮绑定onClick事件,点击按钮时返回到第一页
           .onClick(() => {
             router.back()
           })
         }
         .width('100%')
Z
zengyawen 已提交
267
       }
Z
zengyawen 已提交
268 269
       .height('100%')
     }
Z
zengyawen 已提交
270 271 272
   }
   ```

G
ge-yafang 已提交
273
3. 打开index.ets文件,点击预览器中的![zh-cn_image_0000001311015192](figures/zh-cn_image_0000001311015192.png)按钮进行刷新。效果如下图所示:
274

G
ge-yafang 已提交
275
   ![zh-cn_image_0000001364254773](figures/zh-cn_image_0000001364254773.png)
Z
zengyawen 已提交
276 277 278 279 280 281


## 使用真机运行应用

1. 将搭载OpenHarmony标准系统的开发板与电脑连接。

晴天 已提交
282
2. 点击**File**> **Project Structure...** > **Project**>**SigningConfigs**界面勾选“**Automatically generate signature**”,等待自动签名完成即可,点击“**OK**”。如下图所示:
283

G
ge-yafang 已提交
284
   ![06](figures/06.png)
285

G
ge-yafang 已提交
286
3. 在编辑窗口右上角的工具栏,点击![zh-cn_image_0000001364054485](figures/zh-cn_image_0000001364054485.png)按钮运行。效果如下图所示:
287

G
ge-yafang 已提交
288
   ![zh-cn_image_0000001311334972](figures/zh-cn_image_0000001311334972.png)
289

290
恭喜您已经使用ArkTS语言开发(Stage模型)完成了第一个OpenHarmony应用,快来[探索更多的OpenHarmony功能](../application-dev-guide.md)吧。