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>

效果图

完结

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

相关推荐
李游Leo19 小时前
HarmonyOS 7 API 26 升级适配实战:API Change Assistant、targetSDKVersion 与兼容性回归
harmonyos
OH_TPC19 小时前
HarmonyOS APP开发---"智泊"智能停车App,需要用到这个库
harmonyos
用户1179104883319 小时前
AI粘贴多行需求为什么会误执行?hmharness安全粘贴拆解 GitHub:swsgbl/hmharness
ai编程·harmonyos
用户09340777351419 小时前
HarmonyOS WPS Open SDK 实践:从 HAR 集成到 OpenFileRequest 最小闭环
typescript·harmonyos
威哥爱编程1 天前
HarmonyOS 7 Fast Kit 算法加速实战:四大核心能力,把算力从系统里"借"出来
harmonyos·arkts
威哥爱编程1 天前
HarmonyOS 7 游戏快启实战:Graphics Accelerate Kit 内存镜像秒级启动 + 预启动,把读条变成"秒进"
harmonyos·arkts
木子雨廷1 天前
第 19 天|Preferences:轻量缓存与状态恢复
harmonyos
威哥爱编程1 天前
HarmonyOS 7 精准碰一碰实战:识别触碰坐标,素材精准插入指定位置
harmonyos
二流小码农1 天前
鸿蒙开发:ArrayList,可不会让UI更新哦
android·ios·harmonyos
威哥爱编程1 天前
HarmonyOS 7 数字盾 2.0 实战:可信数字签名、可信 UI 确认与可信输入
harmonyos