[[WARNING]]: It looks like the question TypeScript: add type property to HtmlElement but I need to add a function to an Element Object but not add an attribute to a Node.
I try to make JavaScript to TypeScript but I am new with TypeScript, I try to add a new
function to el.clickOutsideEvent
, but ts do not allow me to do like this:
Vue.directive('click-outside', {
bind: function (el: HTMLElement, binding: Object, vnode: VNode) {
// stack-overflow: https://stackoverflow.com/a/42389266/13031497
el.clickOutsideEvent = function (event: MouseEvent): void {
if (!(el == event.target) ||
event.target instanceof Node && el.contains(event.target)) {
vnode.context[binding.expression](event);
}
}
// ugly, but fix the bug:
// 2020/11/20 - Peterlits Zo
// When I click the the ellipsis-h buttom, it do not change anything.
// I find that itself call this function auto, so I add this after times.
setTimeout(() => {
document.body.addEventListener('click', el.clickOutsideEvent)
}, 50)
},
unbind: function (el: HTMLElement) {
document.body.removeEventListener('click', el.clickOutsideEvent)
},
})
It tell me that: Property 'clickOutsideEvent' does not exist on type 'HTMLElement'.
and something else.
What should I do for it?