一、基于Stage模型构建第一个ArkTS应用

一 工具下载安装

安装最新版DevEco Studio

 

二 创建工程

  1. 若首次打开DevEco Studio,请点击Create Project创建工程。如果已经打开了一个工程,请在菜单栏选择File> New > Create Project来创建一个新工程。
  2. 选择Application应用开发(本文以应用开发为例,Atomic Service对应为元服务开发),选择模板“Empty Ability”,点击Next进行下一步配置。
  3. 进入配置工程界面,Compatible SDK选择“5.0.0(12)”,其他参数保持默认设置即可。
  4. 点击Finish,工具会自动生成示例代码和相关资源,等待工程创建完成。

      ArkTS工程目录结构(Stage模型)

  

  • AppScope > app.json5:应用的全局配置信息,详见app.json5配置文件
  • entry:HarmonyOS工程模块,编译构建生成一个HAP包。 
    • src > main > ets:用于存放ArkTS源码。
    • src > main > ets > entryability:应用/服务的入口。
    • src > main > ets > entrybackupability:应用提供扩展的备份恢复能力。
    • src > main > ets > pages:应用/服务包含的页面。
    • src > main > resources:用于存放应用/服务所用到的资源文件,如图形、多媒体、字符串、布局文件等。关于资源文件,详见资源分类与访问
    • src > main > module.json5:模块配置文件。主要包含HAP包的配置信息、应用/服务在具体设备上的配置信息以及应用/服务的全局配置信息。具体的配置文件说明,详见module.json5配置文件
    • build-profile.json5:当前的模块信息 、编译信息配置项,包括buildOption、targets配置等。
    • hvigorfile.ts:模块级编译构建任务脚本。
    • obfuscation-rules.txt:混淆规则文件。混淆开启后,在使用Release模式进行编译时,会对代码进行编译、混淆及压缩处理,保护代码资产。详见开启代码混淆
    • oh-package.json5:用来描述包名、版本、入口文件(类型声明文件)和依赖项等信息。
  • oh_modules:用于存放三方库依赖信息。
  • build-profile.json5:应用级配置信息,包括签名signingConfigs、产品配置products等。其中products中可配置当前运行环境,默认为HarmonyOS。

  • hvigorfile.ts:应用级编译构建任务脚本。

  • oh-package.json5:主要用来描述全局配置,如:依赖覆盖(overrides)、依赖关系重写(overrideDependencyMap)和参数化配置(parameterFile)等。

  

三 尝试添加内容和添加页面

  1 打开pages目录下Index.ets,添加一些内容

 1 // Index.ets
 2 // 导入页面路由模块
 3 import { router } from '@kit.ArkUI'
 4 import { BusinessError } from '@kit.BasicServicesKit'
 5 
 6 @Entry
 7 @Component
 8 struct Index {
 9   @State message: string = 'Hello World';
10 
11   build() {
12     RelativeContainer() {
13       Column() {
14         Text(this.message)
15           .id('HelloWorld')
16           .fontSize(50)
17           .fontWeight(FontWeight.Bold)
18           .alignRules({
19             center: { anchor: '__container__', align: VerticalAlign.Center },
20             middle: { anchor: '__container__', align: HorizontalAlign.Center }
21           })
22 
23         //添加按钮,以响应用户点击
24         Button() {
25           Text('Next')
26             .fontSize(30)
27             .fontWeight(FontWeight.Bold)
28         }
29         .type(ButtonType.Capsule)
30         .margin({
31           top: 20
32         })
33         .backgroundColor('#0D9FFB')
34         .width('40%')
35         .height('5%')
36         // 跳转按钮绑定onClick事件,点击时跳转到第二页
37         .onClick( () => {
38           console.info(`Succeeded in clicking the 'Next' button. `)
39           // 跳转到第二页
40           router.pushUrl({ url: 'pages/Second' }).then( () => {
41             console.info('Succeeded in jumping to the second page.')
42           }).catch( (err: BusinessError) => {
43             console.error(`Failed to jump to the second page. Code is ${err.code}, message is ${err.message}`)
44           })
45         })
46       }
47       .width('100%')
48     }
49     .height('100%')
50     .width('100%')
51   }
52 }

  在打开Index.ets时,点击 就可以预览。   

  

  2 创建Second.ets,内容如下:

 1 // Second.ets
 2 // 导入页面路由模块
 3 import { router } from '@kit.ArkUI';
 4 import { BusinessError } from '@kit.BasicServicesKit';
 5 
 6 
 7 @Entry
 8 @Component
 9 struct Second {
10   @State message: string = 'Hi there'
11 
12   build() {
13     Row() {
14       Column() {
15         Text(this.message)
16           .fontSize(50)
17           .fontWeight(FontWeight.Bold)
18         Button()  {
19           Text('Back')
20             .fontSize(25)
21             .fontWeight(FontWeight.Bold)
22         }
23         .type(ButtonType.Capsule)
24         .margin({
25           top: 20
26         })
27         .backgroundColor('#0D9FFB')
28         .width('40%')
29         .height('5%')
30         // 返回按钮绑定onClick事件,点击按钮时返回到第一页
31         .onClick(() => {
32           console.info(`Succeeded in clicking the 'Back' button.`)
33           try {
34             // 返回第一页
35             router.back()
36             console.info('Succeeded in returning to the first page.')
37           } catch (err) {
38             let code = (err as BusinessError).code;
39             let message = (err as BusinessError).message;
40             console.error(`Failed to return to the first page. Code is ${code}, message is ${message}`)
41           }
42         })
43       }
44       .width('100%')
45     }
46     .height('100%')
47   }
48 }

  预览

  

   3 配置路由

  在“Project”窗口,打开“entry > src > main > resources > base > profile”,在main_pages.json文件中的“src”下配置第二个页面的路由“pages/Second”。示例如下:

1 {
2   "src": [
3     "pages/Index",
4     "pages/Second"
5   ]
6 }

  刷新预览就可以在预览中切换两个页面了。

四、真机运行

 直接在这里选择你的设备就可以了。

  如果暂时没有鸿蒙手机,可以申请模拟器。具体申请流程,看华为官网。

五、因为对ArkTS还不是很了解,暂不做评价。

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

posted @ 2024-07-02 22:37  上九品  阅读(62)  评论(0)    收藏  举报