uniapp 嵌入鸿蒙原生组件 具体步骤

关于怎么使用uniapp 嵌入鸿蒙原生组件

HBuilder X 版本 4.64



app-harmony文件下新建 index.uts button.ets

button.ets里面复制uniapp 官方提供的 示例代码

https://uniapp.dcloud.net.cn/tutorial/harmony/native-component.html

button.ets

javascript 复制代码
import { NativeEmbedBuilderOptions, defineNativeEmbed } from "@dcloudio/uni-app-runtime"

interface ButtonBuilderOptions extends NativeEmbedBuilderOptions {
  label: string
}

interface ButtonClickEventDetail {
  text: string
}


@Component
struct ButtonComponent {
  @Prop label: string
  onButtonClick?: Function

  build() {
    Button(this.label)
      .width('100%')
      .height('100%')
      .onClick(() => {
        if (this.onButtonClick) {
          const detail = {
            text: 'test'
          } as ButtonClickEventDetail
          this.onButtonClick({
            detail
          })
        }
      })
  }
}

@Builder
function ButtonBuilder(options: ButtonBuilderOptions) {
  ButtonComponent({
    label: options.label,
    onButtonClick: options?.on?.get('click')
  })
    .width(options.width)
    .height(options.height)
}

defineNativeEmbed('button', {
  builder: ButtonBuilder
})

index.vue

javascript 复制代码
<template>
	<embed class="native-button" tag="button" :options="options" @click="onClick"></embed>
</template>

<script setup lang="ts">
import "@/uni_modules/native-harmony-button";
import { ref } from 'vue';
const options = ref({ label: 'hello' })
const onClick = (e) => {
	console.log(e)
}
</script>
<style lang="scss" scoped>
 .native-button {
    display: block;
    width: 200px;
    height: 50px;
    margin: 10px auto;
}
</style>

效果图

完结

效果没出来的可以在评论区提问

相关推荐
2501_9481226312 分钟前
开源鸿蒙平台 CMP 三方库「kmp-showcase-compose」适配全流程
华为·开源·harmonyos
OH_TPC13 分钟前
HarmonyOS APP开发---“旅图“旅行摄影社区App,需要用到这个库
华为·harmonyos·鸿蒙
独隅3 小时前
HarmonyOS 系统能力与体验深度评测
华为·harmonyos·harmonyos 7.0
2601_949575863 小时前
开源鸿蒙平台 CMP 三方库「Persian-Date-picker」适配全流程
华为·开源·harmonyos
KevinChaos4 小时前
灶台菜谱设计手记 02|HarmonyOS 7.0 空间化:菜图滚动实现
harmonyos
独隅5 小时前
HarmonyOS 全场景智能应用落地指南
华为·harmonyos·harmonyos 7.0
熊猫钓鱼>_>5 小时前
鸿蒙PC构建实战:hvigor modelVersion 降级 + schema 清理 + ArkTS 编译三连坑完整复盘
开发语言·华为·ai编程·harmonyos·openharmony·数据·ai生成
2601_949970695 小时前
开源鸿蒙平台 CMP 三方库「SaltUI」适配全流程
华为·开源·harmonyos
马剑威(威哥爱编程)5 小时前
【HarmonyOS 7 小艺智能体实战】06:VibeCoding 十分钟搓出一个 Skill(上)——从想法到生成
人工智能·华为·harmonyos
马剑威(威哥爱编程)6 小时前
【HarmonyOS 7 小艺智能体实战】07:手写一个 Skill(下)——ArkTS、意图框架与工程结构
人工智能·华为·harmonyos