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>

效果图

完结

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

相关推荐
一条可有可无的咸鱼10 小时前
企业招聘信息,企业资讯进行公示
java·vue.js·spring boot·uni-app
waeng_luo11 小时前
[鸿蒙2025领航者闯关]HarmonyOS路由跳转
harmonyos·鸿蒙2025领航者闯关·鸿蒙6实战·开发者年度总结
hh.h.12 小时前
开源鸿蒙生态下Flutter的发展前景分析
flutter·开源·harmonyos
讯方洋哥15 小时前
HarmonyOS应用开发——应用状态
华为·harmonyos
ujainu15 小时前
鸿蒙与Flutter:全场景开发的技术协同与价值
flutter·华为·harmonyos
FrameNotWork16 小时前
HarmonyOS 教学实战:从 0 写一个完整应用(真正能跑、能扩展)
pytorch·华为·harmonyos
Random_index16 小时前
#HarmonyOS篇:鸿蒙开发模板&&三方库axios使用&&跨模块开发交互
harmonyos
游戏技术分享17 小时前
【鸿蒙游戏技术分享 第71期】资质证明文件是否通过
游戏·华为·harmonyos
赵浩生18 小时前
鸿蒙技术干货11:属性动画与转场效果实战
harmonyos
Monkey_2418 小时前
鸿蒙开发工具大全
华为·harmonyos