<template><view><button ref="myButton" @click="handleClick">点击我</button></view>
</template><script>
export default {mounted() {this.$nextTick(() => {const button = this.$refs.myButton;console.log('Button reference:', button);// 检查 button 是否是 HTML 元素if (button && button instanceof HTMLElement) {console.log('Button is an HTMLElement');button.click(); // 模拟点击} else if (button && button.$el) {// 访问 Vue 组件的根元素console.log('Button is a Vue component');button.$el.click(); // 模拟点击} else {console.error('Button reference is not an HTMLElement or is undefined');}});},methods: {handleClick() {console.log('按钮被点击了!');}}
}
</script>
这个只适用于H5页面 编译成微信小程序则无法使用