Typescript 1.5 导出/导入类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31679537/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 03:03:05  来源:igfitidea点击:

Typescript 1.5 exporting/importing classes

typescript

提问by David Laberge

It might be a misunderstanding from my part. In Typescript 1.4 we use to export import classes, but when I updated my code to typescript 1.5 the behavior changed.

这可能是我的误解。在 Typescript 1.4 中,我们使用导出导入类,但是当我将代码更新为 typescript 1.5 时,行为发生了变化。

Here is how it worked in TS 1.4

这是它在 TS 1.4 中的工作方式

LanguageForm.ts

LanguageForm.ts

import AbstractForm = require('../components/AbstractForm');

class LanguageForm extends AbstractForm {
    buildPanel(){

    }
}
export = LanguageForm;

From my understanding in TS 1.5 the syntaxt needs to be modify to :

根据我在 TS 1.5 中的理解,需要将语法修改为:

import AbstractForm from '../components/AbstractForm';
export default class LanguageForm extends AbstractForm {
    buildPanel(){

    }
}

Whith TS1.4 I could simply do a call on newin order for it to work in a dynamic setting :

对于 TS1.4,我可以简单地进行调用new,以便它在动态设置中工作:

require(["LanguageForm"], (Form) => {
    new Form()
});

now in TS 1.5 I need to do :

现在在 TS 1.5 我需要做:

require(["LanguageForm"], (Form) => {
    new Form.default()
});

My questionIn all the example I found the documentation was exporting/importing modules. Is that the way to export/import classes? Can I get rid of the .default?

我的问题在所有示例中,我发现文档是导出/导入模块。这是导出/导入类的方式吗?我可以摆脱.default吗?

回答by basarat

In all the example I found the documentation was exporting/importing modules. Is that the way to export/import classes

在所有示例中,我发现文档是导出/导入模块。这是导出/导入类的方式吗

Don't use export =. Instead export:

不要使用export =. 而是导出:

export class LanguageForm extends AbstractForm {
    buildPanel(){

    }
}

And import:

并导入:

import {LanguageForm} from '../components/LanguageForm';