如何在两个文件中有 class 和 superclass (Typescript)
how to have a class and superclass in two files (Typescript)
如何在 TypeScript 中将 class + subclass 拆分为两个文件?
// MongoModel.js
class MongoModel {
...
}
export = MongoModel;
然后在另一个文件中:
import MongoModel = require("./MongoModel");
但这会出错 File ....MongoModel.ts is not a module
我是否需要使用一些模块语法将它们捆绑在一起,例如 Java 包?
当您在编译器配置中定位 es5
时,您用于导出和导入模块的版本有效。
当定位 es6
时,您有以下 exporting/importing 模块的方式:
// in MongoClass.ts
export class MongoClass {
// ... code here
}
// and in other file
import {MongoClass} from '/path/to/MongoClass';
或者您可以使用 default export
;
// in MongoClass.ts
export default class MongoClass { ... }
export const somethingElse = 5;
// and import in some other file
// note that MongoClass can be renamed when is exported as default exported member
import BaseMongo from '/path/to/MongoClass';
// this cannot be renamed when importing
import {somethingElse} from '/path/to/MongoClass';
如何在 TypeScript 中将 class + subclass 拆分为两个文件?
// MongoModel.js
class MongoModel {
...
}
export = MongoModel;
然后在另一个文件中:
import MongoModel = require("./MongoModel");
但这会出错 File ....MongoModel.ts is not a module
我是否需要使用一些模块语法将它们捆绑在一起,例如 Java 包?
当您在编译器配置中定位 es5
时,您用于导出和导入模块的版本有效。
当定位 es6
时,您有以下 exporting/importing 模块的方式:
// in MongoClass.ts
export class MongoClass {
// ... code here
}
// and in other file
import {MongoClass} from '/path/to/MongoClass';
或者您可以使用 default export
;
// in MongoClass.ts
export default class MongoClass { ... }
export const somethingElse = 5;
// and import in some other file
// note that MongoClass can be renamed when is exported as default exported member
import BaseMongo from '/path/to/MongoClass';
// this cannot be renamed when importing
import {somethingElse} from '/path/to/MongoClass';