[英]How to remove event listener from a web component mixin --> Property 'disconnectedCallback' does not exist on type 'HTMLElement'
I am building a typescript mixin for web components to add drag behvior. 我正在为Web组件构建一个Typescript Mixin,以添加拖动行为。 Until now everything worked pretty nice. 到现在为止,一切工作都还不错。 However I have a difficult problem when trying to remove an event listener after the draggable web component was disconnected. 但是,在可拖动的Web组件断开连接后尝试删除事件侦听器时,我遇到了一个难题。
drag.ts - Adds drag behavior to HTMLElements drag.ts-将拖动行为添加到HTMLElements
type Constructor = new (...args: any[]) => HTMLElement
export function Drag<BC extends Constructor>(BaseClass: BC) {
return class Drag extends BaseClass {
constructor(...args: any[]) {
super(...args)
// Somehow I need to remove this listener when the component is disconnected
document.addEventListener(UPDATE_ACTIVE_DRAG_SET, this.handleToggleDrag)
}
disconnectedCallback() {
// This mehods must be implemented by a web component
// Not all web components that will receive the drag behavior
// will ahve a disconnectedCallback defined
// So typescript must be right when it gives an error
super.disconnectedCallback()
// These work just fine (HTMLElement implements them)
super.innerHTML
super.querySelector('div')
}
// ...more stuff here
}
}
main-menu.ts - Possible candidate for drag behavior main-menu.ts-拖动行为的可能候选者
export class MainMenu extends HTMLElement{
// ... implementation of the web component
// <!> It is very likely that not all components will implement this
disconnectedCallback() {}
}
window.customElements.define('main-menu', DragAndDropLayout(MainMenu))
The only idea I have so far is monkey patching the HTMLElement to include a dummy disconnectedCallback
so that super.disconnectedCallback
will not complain with this error: Property 'disconnectedCallback' does not exist on type 'HTMLElement'
. 我至今唯一的想法是猴子修补HTML元素,包括虚拟disconnectedCallback
使super.disconnectedCallback
不会与此错误抱怨: Property 'disconnectedCallback' does not exist on type 'HTMLElement'
。 I haven't tried it yet. 我还没有尝试过。 Is there a nicer way to fix this? 有没有更好的方法来解决此问题?
Your mixin can test for the presence of a method in a superclass: 您的mixin可以测试超类中是否存在方法:
disconnectedCallback() {
if (super.disconnectedCallback) {
super.disconnectedCallback();
}
// Do mixin work here.
}
This Elix project, which makes heavy use of mixins, explores this topic in its composition rules for mixin methods and properties . 这个Elix项目大量使用mixin,在其mixin方法和属性的组成规则中探讨了该主题。
We use this approach successfully with TypeScript. 我们通过TypeScript成功使用了这种方法。 Note that it's also possible to create TypeScript definition files for your mixin such that TypeScript will know what mixin methods/properties are available to a component using your mixin. 请注意,还可以为您的混合创建TypeScript定义文件,以便TypeScript知道使用您的混合可以对组件使用哪些混合方法/属性。 Our current definition for a Mixin
generic type is at https://github.com/elix/elix/blob/master/src/shared.d.ts . 我们对Mixin
泛型类型的当前定义在https://github.com/elix/elix/blob/master/src/shared.d.ts 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.