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>

效果图

完结

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

相关推荐
用户1129361121910 小时前
没写样式为什么还找我要 wxss?uni-app 这个 ENOENT 坑多数人误判了
前端·微信小程序·uni-app
程序猿追13 小时前
HarmonyOS 7 阅读页换一套配色后怎样记住上次选择
华为·harmonyos
程序猿追14 小时前
HarmonyOS 7 本机书签里,两篇同名文章怎样在重启后各自打开
大数据·数据分析·harmonyos
程序猿追14 小时前
HarmonyOS 7 待办桌面卡片,划掉一项后谁来更新桌面
华为·harmonyos
默 语14 小时前
HarmonyOS7 组件通用事件(三):onAreaChange 区域变化事件——位置动了它知道,offset 动了它装没看见
harmonyos
钉先生贾15 小时前
HarmonyOS 7.0 工程升级实战 49:发布候选与正式配置分叉时,怎样保证可逆回退
华为·harmonyos
OH_TPC17 小时前
@ohos/grpc 源码解读(一):一次 RPC 调用的三层之旅——ArkTS、NAPI 与 nghttp2 帧处理全链路
harmonyos
NJPJI_Yang17 小时前
鸿蒙操作系统使用 Navigation 实现页面路由与栈管理实战(本文已在AtomGit仓库开源)
华为·开源·harmonyos
程序猿追17 小时前
HarmonyOS 7 饮水倒计时归零以后,怎样核对系统收到提醒
华为·harmonyos
黑臂麒麟18 小时前
【鸿蒙心迹】React Native 鸿蒙平板形态适配实战:键盘避让、挖孔安全区与主从栏切换
react native·harmonyos·响应式设计·平板·rnon