提交 1a99a547 编写于 作者: shutao-dc's avatar shutao-dc

native-button.uvue改为组合式 API

上级 144d0b0b
...@@ -12,7 +12,8 @@ ...@@ -12,7 +12,8 @@
return { return {
title: 'Hello', title: 'Hello',
buttonText: "native-button", buttonText: "native-button",
isLoad: false isLoad: false,
clickCount: 0
} }
}, },
onLoad() { onLoad() {
...@@ -23,6 +24,8 @@ ...@@ -23,6 +24,8 @@
uni.showToast({ uni.showToast({
title: "按钮被点击了" title: "按钮被点击了"
}) })
this.clickCount ++
this.buttonText = "native-button"+this.clickCount
}, },
onload() { onload() {
//标记已初始化 用于自动化测试 //标记已初始化 用于自动化测试
......
<template> <template>
<native-view @init="onviewinit" @customClick="ontap"></native-view> <native-view @init="onviewinit" @customClick="ontap"></native-view>
</template> </template>
<script lang="uts"> <script setup lang="uts">
import { NativeButton } from "@/uni_modules/native-button"; import { NativeButton } from "@/uni_modules/native-button";
export default { let button : NativeButton | null = null
data() {
return { //声明属性
button: null as NativeButton | null, const props = defineProps<{ text : string }>()
value: ""
} //声明事件
}, const emit = defineEmits<{
props: { (e : "load") : void
"text": { (e : "buttonTap", event : UniNativeViewEvent) : void
type: String, }>()
default: ''
} //声明方法
}, function updateText(value : string) {
watch: { button?.updateText(value)
"text": { }
handler(newValue : string, oldValue : string) {
this.value = newValue //监听属性变化
this.updateText(newValue) watchEffect(() => {
}, // console.log("watchEffect "+props.text)
immediate: true const text = props.text
}, updateText(text)
}, })
methods: {
//native-view初始化时触发此方法 //native-view初始化时触发此方法
onviewinit(e : UniNativeViewInitEvent) { function onviewinit(e : UniNativeViewInitEvent) {
//获取UniNativeViewElement 传递给NativeButton对象 //获取UniNativeViewElement 传递给NativeButton对象
this.button = new NativeButton(e.detail.element); button = new NativeButton(e.detail.element);
this.button?.updateText(this.value) updateText(props.text)
this.$emit("load") emit("load")
}, }
ontap(e: UniNativeViewEvent) {
this.$emit("buttonTap", e) function ontap(e : UniNativeViewEvent) {
}, emit("buttonTap", e)
updateText(value: string) { }
this.button?.updateText(value)
} function onUnmounted() {
}, // iOS平台需要主动释放 uts 实例
unmounted() { button?.destroy()
// iOS平台需要主动释放 uts 实例 }
this.button?.destroy()
}
}
</script> </script>
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册