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>

效果图

完结

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

相关推荐
LinMin_Rik1 小时前
解决win11专业版HbuilderX编译vue3的uniappX失败问题
uni-app
leon_teacher1 小时前
HarmonyOS 6 ArkUI V2 实战:水墨启动页的 3 段错峰入场动画与 setInterval 进度条
华为·harmonyos
心疼你的一切2 小时前
从零到一:鸿蒙健康监测应用的全流程开发实录
人工智能·华为·harmonyos·鸿蒙·鸿蒙系统
想你依然心痛2 小时前
HarmonyOS 6(API 23)实战:基于Face AR情绪反馈与Body AR手势操控的“光影剪辑台“——PC端沉浸式影视后期系统
华为·ar·harmonyos·悬浮导航·沉浸光感
游戏开发爱好者82 小时前
iOS应用性能监控:Pre-Main与Main函数耗时分析及Time Profiler使用教程
android·ios·小程序·https·uni-app·iphone·webview
leon_teacher2 小时前
HarmonyOS 6 Navigation 实战:NavPathStack 路由架构与 onShown 跨页状态同步方案
华为·架构·harmonyos
号码认证服务2 小时前
企业固话号码认证能覆盖哪些手机品牌?支持华为、小米、OPPO、vivo等机型
服务器·网络·经验分享·python·华为·智能手机·云计算
云水一下3 小时前
下一代防火墙(NGFW)完全解析:从入门到华为eNSP模拟器实战
网络·华为·下一代防火墙
KKei16383 小时前
Flutter for OpenHarmony 编程技能树APP技术文章
flutter·华为·harmonyos
想你依然心痛3 小时前
HarmonyOS 6(API 23)实战:基于Face AR呼吸监测与Body AR姿态引导的“静界空间“——PC端沉浸式冥想疗愈系统
华为·ar·harmonyos·悬浮导航·沉浸光感