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>

效果图

完结

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

相关推荐
威哥爱编程2 小时前
【鸿蒙开发案例篇】拒绝裸奔!鸿蒙6实现PDF动态加密
harmonyos·arkts·arkui
是Yu欸2 小时前
①【openFuyao】智算时代的异构算力连接器
华为·华为云·算力·openfuyao
yilan_n5 小时前
在Linux下使用Termony搭建HNP自验证环境全指南
linux·harmonyos·termony·hnp·命令行适配
志尊宝7 小时前
HarmonyOS ArkTS 入门(一)
华为·harmonyos
90后的晨仔7 小时前
🧩 一文搞懂 HarmonyOS 中的 HAP、HAR 和 HSP:它们到底是什么?怎么用?
harmonyos
m0_685535088 小时前
光是如何产生的?
华为·光学·光学设计·光学工程·镜头设计
Simon席玉8 小时前
C++的命名重整
开发语言·c++·华为·harmonyos·arkts
我是Feri9 小时前
HarmonyOS 6.0 视频播放组件使用教程
华为·音视频·harmonyos
2501_9159090610 小时前
手机崩溃日志导出的工程化体系,从系统级诊断到应用行为分析的多工具协同方法
android·ios·智能手机·小程序·uni-app·iphone·webview
大土豆的bug记录10 小时前
鸿蒙权限授权弹窗(推荐)
华为·harmonyos