首页 文章

打字稿的不同导入和导出形式

提问于
浏览
2

打字稿中我是一个不高兴的人 . 任何人都可以向我解释这些出口之间的区别:

export default class Foo {}
/* or */
class Foo {}
export = Foo;
/* or */
export class Foo { }

以及这些导入形式之间的区别:

import x = require('y');
import x from 'y';
import { x } from 'y'
import * as x from 'y';

何时使用它们?

1 回答

  • 2

    它全部包含在_3001191中:

    Default exports

    export default class Foo {}
    // and
    import x from 'y';
    

    每个模块都可以选择导出默认导出 . 默认导出标有关键字default;并且每个模块只能有一个默认导出 . 使用不同的导入表单导入默认导出 .

    Export =

    export = Foo;
    // and
    import x = require('y');
    

    CommonJS和AMD通常都有一个export对象的概念,它包含模块的所有导出 . 它们还支持使用自定义单个对象替换导出对象 . 默认导出旨在替代此行为;但是,这两者是不相容的 . TypeScript支持export =来模拟传统的CommonJS和AMD工作流程 . export =语法指定从模块导出的单个对象 . 这可以是类,接口,命名空间,函数或枚举 . 使用export =导入模块时,必须使用TypeScript特定的import let = require(“module”)来导入模块 .

    您包含的其他表格:

    export class Foo { }
    // and
    import { x } from 'y'
    import * as x from 'y';
    

    exportimport的正常形式 .
    它基于es6导入/导出语法,您可以在MDN中找到更多信息:import / export .

相关问题