start-with-ets-fa.md 9.9 KB
Newer Older
G
ge-yafang 已提交
1
# 使用eTS语言开发(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.991 Beta4**版本为例,点击[此处](https://developer.harmonyos.com/cn/develop/deveco-studio#download_beta)获取下载链接。
Z
zengyawen 已提交
8 9 10 11


## 创建eTS工程

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

G
ge-yafang 已提交
15 16
2. 进入配置工程界面,**Compile SDK** 选择“**8**”(**Complie SDK**选择“**9**”时注意同步选择**Model** 为“**FA**”,此处以选择“**8**”为例),**Language**选择“**eTS**”,其他参数保持默认设置即可。
   ![02](figures/02.png)
17

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

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


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

G
ge-yafang 已提交
30 31 32 33 34 35 36 37 38 39
- **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 已提交
40

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

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


## 构建第一个页面

48
1. 使用文本组件。
G
ge-yafang 已提交
49
   工程同步完成后,在“**Project**”窗口,点击“**entry > src > main > ets > MainAbility > pages**”,打开“**index.ets**”文件,可以看到页面由Text组件组成。“**index.ets**”文件的示例如下:
Z
zengyawen 已提交
50 51

   
52 53
   ```ts
   // index.ets
Z
zengyawen 已提交
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
   @Entry
   @Component
   struct Index {
     @State message: string = 'Hello World'
   
     build() {
       Row() {
         Column() {
           Text(this.message)
             .fontSize(50)
             .fontWeight(FontWeight.Bold)
         }
         .width('100%')
       }
       .height('100%')
     }
   }
   ```

73
2. 添加按钮。
G
ge-yafang 已提交
74
   在默认页面基础上,我们添加一个Button组件,作为按钮响应用户点击,从而实现跳转到另一个页面。“**index.ets**”文件的示例如下:
Z
zengyawen 已提交
75 76

   
77 78
   ```ts
   // index.ets
Z
zengyawen 已提交
79 80 81 82 83 84 85 86 87 88 89
   @Entry
   @Component
   struct Index {
     @State message: string = 'Hello World'
   
     build() {
       Row() {
         Column() {
           Text(this.message)
             .fontSize(50)
             .fontWeight(FontWeight.Bold)
G
ge-yafang 已提交
90
           // 添加按钮,以响应用户点击
Z
zengyawen 已提交
91
           Button() {
Z
zengyawen 已提交
92
             Text('Next')
Z
zengyawen 已提交
93 94 95 96
               .fontSize(30)
               .fontWeight(FontWeight.Bold)
           }
           .type(ButtonType.Capsule)
Z
zengyawen 已提交
97
           .margin({
Z
zengyawen 已提交
98
             top: 20
Z
zengyawen 已提交
99 100 101 102 103 104 105 106 107 108 109 110
           })
           .backgroundColor('#0D9FFB')
           .width('40%')
           .height('5%')
         }
         .width('100%')
       }
       .height('100%')
     }
   }
   ```

111
3. 在编辑窗口右上角的侧边工具栏,点击Previewer,打开预览器。第一个页面效果如下图所示:
G
ge-yafang 已提交
112
   ![zh-cn_image_0000001364254741](figures/zh-cn_image_0000001364254741.png)
Z
zengyawen 已提交
113 114 115 116


## 构建第二个页面

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

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

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

141
2. 添加文本及按钮。
Z
zengyawen 已提交
142 143
   参照第一个页面,在第二个页面添加Text组件、Button组件等,并设置其样式。“**second.ets**”文件的示例如下:

G
ge-yafang 已提交
144
   
145 146
   ```ts
   // second.ets
Z
zengyawen 已提交
147 148 149 150
   @Entry
   @Component
   struct Second {
     @State message: string = 'Hi there'
Z
zengyawen 已提交
151 152 153 154 155 156
   
     build() {
       Row() {
         Column() {
           Text(this.message)
             .fontSize(50)
Z
zengyawen 已提交
157
             .fontWeight(FontWeight.Bold)
Z
zengyawen 已提交
158 159 160 161 162 163 164 165 166 167 168 169 170 171
           Button() {
             Text('Back')
               .fontSize(25)
               .fontWeight(FontWeight.Bold)
           }
           .type(ButtonType.Capsule)
           .margin({
             top: 20
           })
           .backgroundColor('#0D9FFB')
           .width('40%')
           .height('5%')
         }
         .width('100%')
Z
zengyawen 已提交
172
       }
Z
zengyawen 已提交
173
       .height('100%')
Z
zengyawen 已提交
174 175 176 177 178 179 180
     }
   }
   ```


## 实现页面间的跳转

181
页面间的导航可以通过页面路由router来实现。页面路由router根据页面url找到目标页面,从而实现跳转。使用页面路由请导入router模块。
Z
zengyawen 已提交
182

183
1. 第一个页面跳转到第二个页面。
184
   在第一个页面中,跳转按钮绑定onClick事件,点击按钮时跳转到第二页。“**index.ets**”文件的示例如下:
Z
zengyawen 已提交
185 186

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

227
2. 第二个页面返回到第一个页面。
228
   在第二个页面中,返回按钮绑定onClick事件,点击按钮时返回到第一页。“**second.ets**”文件的示例如下:
Z
zengyawen 已提交
229 230

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

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


## 使用真机运行应用

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

G
ge-yafang 已提交
278 279
2. 点击**File**> **Project Structure...** > **Project**>**SigningConfigs** 界面勾选“**Automatically generate signing**”,等待自动签名完成即可,点击“**OK**”。如下图所示:
   ![06](figures/06.png)
280

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

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