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

G
ge-yafang 已提交
3 4

>  **说明:**
Z
zengyawen 已提交
5 6
> 请使用**DevEco Studio V3.0.0.601 Beta1**及更高版本。
> 
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)
Z
zengyawen 已提交
15

16
2. 进入配置工程界面,**Compile SDK** 选择“**8**”(**Compile SDK**选择“**9**”时注意同步选择**Model** 为“**FA**”,此处以选择“**8**”为例),**Language**选择“**eTS**”,其他参数保持默认设置即可。
17

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

G
ge-yafang 已提交
20
   > **说明:**
21
   > DevEco Studio V3.0 Beta3及更高版本支持使用ArkTS[低代码开发](https://developer.harmonyos.com/cn/docs/documentation/doc-guides/ohos-low-code-development-0000001218440652)方式。
G
ge-yafang 已提交
22 23 24 25
   > 
   > 低代码开发方式具有丰富的UI界面编辑功能,通过可视化界面开发方式快速构建布局,可有效降低开发者的上手成本并提升开发者构建UI界面的效率。
   > 
   > 如需使用低代码开发方式,请打开上图中的Enable Super Visual开关。
Z
zengyawen 已提交
26 27 28 29

3. 点击**Finish**,工具会自动生成示例代码和相关资源,等待工程创建完成。


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

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

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

G
ge-yafang 已提交
45
- **hvigorfile.js**:应用级编译构建任务脚本。
Z
zengyawen 已提交
46 47 48 49


## 构建第一个页面

50
1. 使用文本组件。
51

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

75
2. 添加按钮。
76

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

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

G
ge-yafang 已提交
115
   ![zh-cn_image_0000001364254741](figures/zh-cn_image_0000001364254741.png)
Z
zengyawen 已提交
116 117 118 119


## 构建第二个页面

120
1. 创建第二个页面。
121

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

G
ge-yafang 已提交
124
      ![zh-cn_image_0000001311334932](figures/zh-cn_image_0000001311334932.png)
125

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

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

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


## 实现页面间的跳转

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

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

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

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

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

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

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


## 使用真机运行应用

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

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

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

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

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

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