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>

效果图

完结

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

相关推荐
OH_TPC8 小时前
HarmonyOS APP开发---“滤镜大师“图像处理App,需要用到这个库
java·图像处理·华为·harmonyos·鸿蒙
2501_9197490311 小时前
华为鸿蒙管理密码APP—小羊密码
华为·harmonyos·鸿蒙
ITUnicorn14 小时前
【HarmonyOS】时间管理类APP:做成“自适应“
harmonyos
笔触狂放14 小时前
第2章 ArkTS(上)
华为·harmonyos·鸿蒙
新元代码15 小时前
探秘鸿蒙南向开发:Hi3861 架构、编译与实战全解析
华为·架构·harmonyos
笔触狂放16 小时前
第1章 初识鸿蒙
华为·harmonyos
xiaoxiangsiyan16 小时前
企业园区局域网交换技术完整版手册主打华为设备
运维·网络·学习·华为·路由·交换机·企业网
小雨青年17 小时前
【HarmonyOS 7 沉浸光感深度实战】 06 复杂背景下的自动反色与可读性处理
华为·harmonyos
水深火乐18 小时前
HmarkX(码笺)的本地数据库模块重构实战
harmonyos
大锅盖118 小时前
Map 搜索没有返回可信候选时,派单页面该怎么继续工作
华为·harmonyos