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>

效果图

完结

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

相关推荐
游九尘13 分钟前
uniapp获取定位uni.getLocation报错getLocation:fail maybe not obtain GPS Permission.
uni-app
雪芽蓝域zzs10 小时前
uniapp 该应用与此设备的CPU不兼容
uni-app
刘大猫.12 小时前
华为昇腾芯片将为DeepSeek-V4推理,通往国产算力自由
华为·ai·大模型·算力·deepseek·deepseek-v4·昇腾芯片
CHB13 小时前
uni-task - 轻量级团队任务管理系统
uni-app
Lanren的编程日记18 小时前
Flutter鸿蒙应用开发:数据加密功能实现实战,全方位保护用户隐私数据
flutter·华为·harmonyos
想你依然心痛18 小时前
HarmonyOS 6健康应用实战:基于悬浮导航与沉浸光感的“光影律动“智能健身系统
华为·harmonyos·悬浮导航·沉浸光感
酣大智18 小时前
Win11 24H2 eNSP中AR报错40,解决方法
网络·华为
ICT系统集成阿祥19 小时前
黄金秘籍解决华为防火墙最困难的故障
网络·华为·php
酣大智20 小时前
eNSP中AR报错40,重新安装
网络·华为
weitingfu20 小时前
AI 游戏,为什么更适合鸿蒙?
人工智能·游戏·华为·ai·harmonyos