基于DevEco Studio的OpenHarmony应用原子化服务(元服务)入门教程

一、创建项目

二、创建卡片

三、应用服务代码

Index.ets

复制代码
@Entry
@Component
struct Index {
  @State TITLE: string = 'OpenHarmony';
  @State CONTEXT: string = '创新召见未来!';

  build() {
    Row() {
      Column() {
        Text(this.TITLE)
          .fontSize(30)
          .fontColor(0xFEFEFE)
          .fontWeight(600)
        Text(this.CONTEXT)
          .fontSize(30)
          .fontColor(0xFEFEFE)
          .fontWeight(600)
          .margin(20)
      }
      .width('100%')
    }
    .width('100%')
    .height('100%')
    .backgroundColor(0x42218c)

  }
}

四、卡片代码

WidgetNewCard.ets

复制代码
@Entry
@Component
struct WidgetNewCard {
  /*
   * The title.
   */
  readonly TITLE: string = 'OpenHarmony';
  readonly CONTEXT: string = '技术构建万物智联';
  /*
   * The action type.
   */
  readonly ACTION_TYPE: string = 'router';

  /*
   * The ability name.
   */
  readonly ABILITY_NAME: string = 'EntryAbility';

  /*
   * The message.
   */
  readonly MESSAGE: string = 'add detail';

  /*
   * The with percentage setting.
   */
  readonly FULL_WIDTH_PERCENT: string = '100%';

  /*
   * The height percentage setting.
   */
  readonly FULL_HEIGHT_PERCENT: string = '100%';

  build() {
    Row() {
      Column() {
        Text(this.TITLE)
          .fontSize(14)
          .fontColor(0xFEFEFE)
          .fontWeight(600)
        Text(this.CONTEXT)
          .fontSize(14)
          .fontColor(0xFEFEFE)
          .fontWeight(600)
      }
      .width(this.FULL_WIDTH_PERCENT)
    }
    .backgroundColor(0x42218c)
    .height(this.FULL_HEIGHT_PERCENT)
    .onClick(() => {
      postCardAction(this, {
        "action": this.ACTION_TYPE,
        "abilityName": this.ABILITY_NAME,
        "params": {
          "message": this.MESSAGE
        }
      });
    })
  }
}

五、运行案例

六、案例卡片效果

注释说明:

现最新版的DevEco Studio中OpenHarmony项目在本目录下调整。将runtimeOS:""中间内容调为OpenHarmony即可。这种方式,不用开发板,通过预览器就可以查看项目样式效果。

完整项目代码地址:

HarmonyOSAPP开发相关组件: 深圳市蛟龙腾飞网络科技有限公司 - Gitee.com

相关推荐
colicode17 分钟前
Objective-C语音验证码接口API示例代码:老版iOS应用接入语音验证教程
前端·c++·ios·前端框架·objective-c
小圣贤君21 分钟前
从「脑内人设」到「一眼入魂」:51mazi 小说人物图 AI 生成实战
前端·人工智能·文生图·ai写作·通义万相·写作软件·小说人物
SuperEugene25 分钟前
《this、箭头函数与普通函数:后台项目里最容易写错的几种场景》
前端·javascript
Jing_Rainbow26 分钟前
【React-11/Lesson95(2026-01-04)】React 闭包陷阱详解🎯
前端·javascript·react.js
麦芽糖021934 分钟前
微信小程序七-2 npm包以及全局数据共享
前端·小程序·npm
Zhencode39 分钟前
深入Vue3响应式核心:computed 的实现原理与应用
前端·javascript·vue.js
独自破碎E1 小时前
【滑动窗口】BISHI47 交换到最大
java·开发语言·javascript
剑亦未配妥1 小时前
CSS 折叠引发的 scrollHeight 异常 —— 一次 Blink 引擎的诡异 Bug
前端·css·bug
CappuccinoRose1 小时前
HTML语法学习文档(三)
前端·学习·html·html5·标签·实体字符