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>

效果图

完结

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

相关推荐
iOS阿玮20 小时前
碰到一个不听劝的老板,喜提4.3a!
uni-app·app·apple
遇到困难睡大觉哈哈1 天前
Harmony os——ArkTS 语言笔记(六):模块、导入导出与 `this` 关键字
笔记·harmonyos·鸿蒙
Brianna Home1 天前
[鸿蒙2025领航者闯关] 鸿蒙 6.0 星盾安全架构 + AI 防窥:金融级支付安全实战与深度踩坑实录
人工智能·安全·harmonyos·安全架构
bestadc1 天前
鸿蒙应用开发的MVVM(Model-View-ViewModel)模式
华为·harmonyos
HONG````1 天前
鸿蒙Tabs组件深度实战:构建流畅的多页面导航与状态保持方案
华为·harmonyos
ZWaruler1 天前
鸿蒙os 配置打包后的文件名称
华为·harmonyos·鸿蒙系统
Devil枫1 天前
[鸿蒙2025领航者闯关]从小白到鸿蒙星炬手
华为·harmonyos
遇到困难睡大觉哈哈1 天前
Harmony os ——ArkTS 语言笔记(五):泛型、空安全与可选链
前端·笔记·安全·harmonyos·鸿蒙
聊询QQ:276998851 天前
基于Matlab的转速开环恒压频比异步电动机调速系统设计报告与仿真程序
uni-app
财经三剑客1 天前
鸿蒙智行全系11月交付81864台 同比增长89.61%
华为·harmonyos