native-button.uvue 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. <template>
  2. <!--建议只存在一个根节点,如果native-view存在兄弟节点则需要将其包裹处理-->
  3. <view>
  4. <native-view style="height: 100px;" @init="onviewinit" @customClick="ontap"></native-view>
  5. <view style="width: 50%;height: 100px;">
  6. <button>测试按钮</button>
  7. </view>
  8. </view>
  9. </template>
  10. <script setup lang="uts">
  11. import { NativeButton } from "@/uni_modules/native-button";
  12. let button : NativeButton | null = null
  13. //声明属性
  14. const props = defineProps<{ text : string }>()
  15. //声明事件
  16. const emit = defineEmits<{
  17. (e : "load") : void
  18. (e : "buttonTap", event : UniNativeViewEvent) : void
  19. }>()
  20. //声明方法
  21. function updateText(value : string) {
  22. button?.updateText(value)
  23. }
  24. //监听属性变化
  25. watchEffect(() => {
  26. const text = props.text
  27. updateText(text)
  28. })
  29. //native-view初始化时触发此方法
  30. function onviewinit(e : UniNativeViewInitEvent) {
  31. //获取UniNativeViewElement 传递给NativeButton对象
  32. button = new NativeButton(e.detail.element);
  33. updateText(props.text)
  34. emit("load")
  35. }
  36. function ontap(e : UniNativeViewEvent) {
  37. emit("buttonTap", e)
  38. }
  39. function onUnmounted() {
  40. // iOS平台需要主动释放 uts 实例
  41. button?.destroy()
  42. }
  43. </script>