typescript 打字稿。如何在一个模块中导出两个类(在单独的文件中)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29168813/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Typescript. How to export two classes (in separate files) in one module?
提问by indapublic
I have two classes declared in two separate files.
我在两个单独的文件中声明了两个类。
a.ts
a.ts
export class AClass {
public constructor () {
console.log('AClass');
}
}
b.ts
b.ts
export class BClass {
public constructor () {
console.log('BClass');
}
}
I want to merge them in one module. How I can realise it?
我想将它们合并到一个模块中。我怎么能意识到呢?
///<reference path='a.ts' />
///<reference path='b.ts' />
module Common {
export class A extends AClass {}
export class B extends BClass {}
}
says:
说:
Cannot find name 'AClass'.
找不到名称“AClass”。
and
和
Cannot find name 'BClass'.
找不到名称“BClass”。
I can import classes
我可以导入类
import AClass = require('a');
import BClass = require('b');
module Common {
}
But how I can correctly export them?
但是我如何正确导出它们?
Cannot find any information in documentation. Please, tell me the best way to realise declarations in one module? Thank you in advance
在文档中找不到任何信息。请告诉我在一个模块中实现声明的最佳方法?先感谢您
采纳答案by Rafal
If you declare class like you showed you include it in the 'global' namespace. To declare class inside a module just wrap it in module declaration:
如果你像你展示的那样声明类,你将它包含在“全局”命名空间中。要在模块中声明类,只需将其包装在模块声明中:
module Common{
export class ClassA{}
}
you can redeclare module in multiple files only one javascript object will be created for the module.
您可以在多个文件中重新声明模块,只会为该模块创建一个 javascript 对象。
回答by Josh Wulf
I do it like this:
我这样做:
m/a.ts
米/吨
export class A {
}
m/b.ts
m/b.ts
export class B {
}
m/index.ts
米/指数.ts
export { A } from './a.ts';
export { B } from './b.ts';
And then I do: consumer.ts
然后我做: consumer.ts
import { A, B } from './m';
回答by Daniel Earwicker
You have export
in front of your class declarations:
你export
在你的类声明前面有:
export class AClass {
This turns that source file into an external module. This means that you will need to use import
/require
from another module:
这会将源文件转换为外部模块。这意味着您需要从另一个模块使用import
/ require
:
import a = require("a");
module Common {
export class A extends a.AClass {}
}
Note that AClass
appears to be a member of a
because that's what I imported its containing module as.
请注意,它AClass
似乎是 的成员,a
因为这是我将其包含模块导入的内容。
Alternatively you could rename a
module after a single class that it contains, e.g.
或者,您可以a
在它包含的单个类之后重命名模块,例如
AClass.ts
AClass.ts
class AClass {
public constructor () {
console.log('AClass');
}
}
export = AClass;
By "assigning" to export
we make that class be the entire single output of module. Hence in another module:
通过“分配”,export
我们使该类成为模块的整个单一输出。因此在另一个模块中:
import AClass = require("AClass");
var a = new AClass(); // no prefix needed
This can be tidier if your module only exports a single class (or function).
如果您的模块只导出单个类(或函数),这会更整洁。