document.addEventListener绑定事件无法生效 控制台没有输出信息
来源:4-10 Dropdown 组件点击外部区域自动隐藏
黔娄
2022-10-13
<template>
<div class="dropdown" ref="dropdownRef">
<a href="#" class="btn btn-outline-light my-2 dropdown-toggle" @click.prevent="toggleOpen">{{title}}</a>
<ul class="dropdown-menu" :style="{display: 'block'}" v-if="isOpen">
<slot></slot>
</ul>
</div>
</template>
<script lang="ts">
import { computed, defineComponent, ref, onMounted, onUnmounted } from 'vue'
export default defineComponent({
name: 'DropDown',
props: {
title: {
type: String,
required: true
}
},
setup () {
const isOpen = ref(false)
const dropdownRef = ref<null | HTMLElement>(null)
const toggleOpen = () => {
isOpen.value = !isOpen.value
}
const handler = (e: MouseEvent) => {
console.log('点击')
if (dropdownRef.value) {
console.log(dropdownRef.value)
}
}
onMounted(() => {
document.addEventListener('clcik', handler)
})
onUnmounted(() => {
document.removeEventListener('click', handler)
})
return {
isOpen,
toggleOpen,
dropdownRef
}
}
})
</script>
<style>
</style>
写回答
2回答
-
张轩
2022-10-15
同学你好
我这里提示你回复了问题 但是看不到内容 是不是被系统过滤掉了?
012022-10-15 -
张轩
2022-10-14
同学你好
可以把源代码(git)提供给我一下 我在本地帮你看下,注意发链接的时候将协议去掉,要不会被系统删除掉
012022-10-14
相似问题