Javascript 在TypeScript中将类转换为模块
我有一门课:Javascript 在TypeScript中将类转换为模块,javascript,typescript,Javascript,Typescript,我有一门课: class Dice { div: Element; value: string; constructor(div: Element) { this.div = div; } } 我有dicerroller类扩展了Dice类: class diceRoller extends Dice { static Values = Values; constructor(div: Element) { supe
class Dice {
div: Element;
value: string;
constructor(div: Element) {
this.div = div;
}
}
我有dicerroller
类扩展了Dice
类:
class diceRoller extends Dice {
static Values = Values;
constructor(div: Element) {
super(div);
(this.div as HTMLElement).style.width = diceSize;
(this.div as HTMLElement).style.height = diceSize;
(this.div as HTMLElement).style.textAlign = 'center';
(this.div as HTMLElement).style.lineHeight = diceSize;
(this.div as HTMLElement).style.border = diceBorder;
(this.div as HTMLElement).style.marginRight = '10px';
(this.div as HTMLElement).style.cssFloat = 'left';
}
changeValue(val: number): boolean {
this.value = Values[val];
(this.div as HTMLElement).innerHTML = this.value;
return true;
}
}
如何将类转化为模块。我已经在打字本手册中尝试了所有的方法,但我还没有弄明白
我想以类似于export{new diceRoller(div:Element)}的形式导出类,因为我想在导入模块的文件中向类传递一个参数。那么,具体的形式是什么
简单地说:
export const foo = new diceRoller(div);
我想以类似于export{new diceRoller(div:Element)}的形式导出类,因为我想在导入模块的文件中向类传递一个参数。那么,具体的形式是什么
简单地说:
export const foo = new diceRoller(div);
只需将其导出即可<代码>导出类diceRoller…
和导出类Dice…
我想以类似于导出{new diceRoller(div:Element)}
的形式导出类,因为我想在导入模块的文件中向类传递一个参数。那么,具体的形式是什么呢?只需将其导出即可<代码>导出类diceRoller…和导出类Dice…
我想以类似于导出{new diceRoller(div:Element)}
的形式导出类,因为我想在导入模块的文件中向类传递一个参数。那么,确切的形式是什么呢?非常感谢,我自己弄明白了。我只是在类之前添加了export default
,我试图使用您的解决方案,但是TypeScript在参数div
上标记了一个错误:找不到名称“div”
非常感谢,我自己找到了答案。我只是在类之前添加了export default
,我试图使用您的解决方案,但TypeScript在参数div
上标记了一个错误:找不到名称“div”