























这是一个创建于 790 天前的主题,其中的信息可能已经有所发展或是发生改变。
我有两个抽象类,但是其结构是一样的,不同的是分别继承了不同的类,我要怎么简写这种情况。
export abstract class DivComponentsManager extends HTMLElement {
protected themeWatcher = themeWatcher
protected disposeFuncs: (() => void)[] = []
constructor() {
super()
}
abstract renderView(): Promise<void>
connectedCallback() {
this.renderView()
}
disconnectedCallback() {
this.disposeFuncs.forEach(func => func())
}
}
export abstract class ButtonComponentsManager extends HTMLButtonElement {
protected themeWatcher = themeWatcher
protected disposeFuncs: (() => void)[] = []
constructor() {
super()
}
abstract renderView(): Promise<void>
connectedCallback() {
this.renderView()
}
disconnectedCallback() {
this.disposeFuncs.forEach(func => func())
}
}
1 ChrisFreeMan 2024 年 4 月 16 日没事...突然想起 gpt3.5 免费了,直接问出了答案。 import { themeWatcher } from './themeWatcher'; // Import themeWatcher if not already imported export abstract class ComponentsManager<T extends HTMLElement | HTMLButtonElement> extends T { constructor() { abstract renderView(): Promise<void>; connectedCallback() { disconnectedCallback() { |
4 Opportunity 2024 年 4 月 16 日function ComponentsManager<T extends (new (...args: any[]) => HTMLElement)>(base: T) { abstract renderView(): Promise<void>; connectedCallback() { export abstract class DivComponentsManager extends ComponentsManager(HTMLDivElement) { export abstract class ButtonComponentsManager extends ComponentsManager(HTMLButtonElement) { |
6 sapjax 2024 年 4 月 16 日HTMLButtonElement 为什么不继承 HTMLElement ? |
8 ChrisFreeMan 2024 年 4 月 16 日@sapjax 这是 web components 有一些情况子类需要调用元素自身的方法,这只是其中一个例子,比如还有 HTMLDialogElement, HTMLCanvasElement |
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。