基于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

相关推荐
PineappleCoder1 天前
性能数据别再瞎轮询了!PerformanceObserver 异步捕获 LCP/CLS,不卡主线程
前端·性能优化
PineappleCoder1 天前
告别字体闪烁 / 首屏卡顿!preload 让关键资源 “高优先级” 提前到
前端·性能优化
m0_471199631 天前
【vue】通俗详解package-lock文件的作用
前端·javascript·vue.js
GIS之路1 天前
GDAL 读取KML数据
前端
今天不要写bug1 天前
vue项目基于vue-cropper实现图片裁剪与图片压缩
前端·javascript·vue.js·typescript
用户47949283569151 天前
记住这张时间线图,你再也不会乱用 useEffect / useLayoutEffect
前端·react.js
咬人喵喵1 天前
14 类圣诞核心 SVG 交互方案拆解(附案例 + 资源)
开发语言·前端·javascript
问君能有几多愁~1 天前
C++ 日志实现
java·前端·c++
咬人喵喵1 天前
CSS 盒子模型:万物皆是盒子
前端·css