start-with-ets-stage.md 9.8 KB
Newer Older
G
ge-yafang 已提交
1
# 使用eTS语言开发(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.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)
14

G
ge-yafang 已提交
15 16
2. 进入配置工程界面,**Compile SDK**选择“**9**”,**Model** 选择“**Stage**”,**Language** 选择“**eTS**”,其他参数保持默认设置即可。
   ![07](figures/07.png)
Z
zengyawen 已提交
17

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

Z
zengyawen 已提交
28

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

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

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

G
ge-yafang 已提交
35 36 37 38 39 40 41 42 43 44
- **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包含的页面。
  - **src > main > resources**:用于存放应用/服务所用到的资源文件,如图形、多媒体、字符串、布局文件等。关于资源文件,详见[资源文件的分类](../ui/ui-ts-basic-resource-file-categories.md)
  - **src > main > module.json5**:模块配置文件。主要包含HAP包的配置信息、应用/服务在具体设备上的配置信息以及应用/服务的全局配置信息。具体的配置文件说明,详见[应用包结构配置文件的说明(Stage模型)](stage-structure.md)
  - **build-profile.json5**:当前的模块信息 、编译信息配置项,包括buildOption、targets配置等。
  - **hvigorfile.js**:模块级编译构建任务脚本,开发者可以自定义相关任务和代码实现。
Z
zengyawen 已提交
45

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

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


## 构建第一个页面

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

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

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

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

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


## 构建第二个页面

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

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

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

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

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


## 实现页面间的跳转

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

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

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

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

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

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


## 使用真机运行应用

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

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

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

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