2020-08-25 22:25:46 +08:00
|
|
|
import { watch } from 'vue'
|
|
|
|
import { on, off } from '@element-plus/utils/dom'
|
|
|
|
|
|
|
|
import type { Ref } from 'vue'
|
|
|
|
|
2020-08-28 10:47:02 +08:00
|
|
|
export type Event = {
|
2020-08-25 22:25:46 +08:00
|
|
|
name: string
|
|
|
|
handler: (...args: any[]) => any
|
2020-08-28 10:47:02 +08:00
|
|
|
}
|
2020-08-25 22:25:46 +08:00
|
|
|
|
2020-08-28 10:47:02 +08:00
|
|
|
export default (el: Ref<HTMLElement>, events: Event[]) => {
|
2020-08-25 22:25:46 +08:00
|
|
|
|
2020-08-28 10:47:02 +08:00
|
|
|
watch(el, val => {
|
|
|
|
if (val) {
|
2021-04-13 17:52:51 +08:00
|
|
|
events.forEach(({ name, handler }) => {
|
2020-08-28 10:47:02 +08:00
|
|
|
on(el.value, name, handler)
|
|
|
|
})
|
|
|
|
} else {
|
2021-04-13 17:52:51 +08:00
|
|
|
events.forEach(({ name, handler }) => {
|
2020-08-25 22:25:46 +08:00
|
|
|
off(el.value, name, handler)
|
|
|
|
})
|
2020-08-28 10:47:02 +08:00
|
|
|
}
|
2020-08-25 22:25:46 +08:00
|
|
|
})
|
|
|
|
}
|