Typescript 1.5 导出/导入类

标签 typescript

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

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

LanguageForm.ts

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

class LanguageForm extends AbstractForm {
    buildPanel(){

    }
}
export = LanguageForm;

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

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

    }
}

在 TS1.4 中,我可以简单地调用 new 以使其在动态设置中工作:

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

现在在 TS 1.5 中我需要做的:

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

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

最佳答案

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

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

export class LanguageForm extends AbstractForm {
    buildPanel(){

    }
}

并导入:

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

关于Typescript 1.5 导出/导入类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31679537/

相关文章:

reactjs - 如果 NOT 可选变量未定义则跳过查询

javascript - filereader.onload 中的函数未在 javascript 中执行

javascript - Typescript async/await 转换为 yield 错误

javascript - 使用 Angular 天数中的两个日期之间的差异

使用 TypeScript 的联合类型时 Angular 模板检查错误

在定义返回接口(interface)的函数时,Typescript 的行为似乎不一致

angular - 在运行时动态更改 Angular 7 中的语言环境,而无需重新加载应用程序

typescript 枚举默认值

typescript - 不能在 Typescript Map 中使用数字作为键?

javascript - 如何从本地存储中获取数据并以 Angular 显示在表格中?