作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 TypeScript 构建 Node 项目。当 TS 将我的类编译为 JS 时,似乎导出了不需要的额外深度,请允许我说明一下。
TS
export class AppInfo {
public name: string;
public version: string;
constructor(name: string, version: string) {
this.name = name;
this.version = version;
}
}
生成的JS
var AppInfo = (function () {
function AppInfo(name, version) {
this.name = name;
this.version = version;
}
return AppInfo;
})();
exports.AppInfo = AppInfo;
所以如果我想导入这个类,我必须这样做:
const AppInfo = require("./AppInfo").AppInfo;
var appInfo = new AppInfo(...);
如何告诉 TypeScript 直接导出我的类?像这样:
exports = AppInfo;
最佳答案
exports = AppInfo;
使用export =
,如下所示:
class AppInfo {
public name: string;
public version: string;
constructor(name: string, version: string) {
this.name = name;
this.version = version;
}
}
export = AppInfo;
关于javascript - 如何将类导出为根模块?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35121795/
我是一名优秀的程序员,十分优秀!