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

时间:2017-02-18 15:09:23

标签: typescript typescript2.0

我是打字稿的声音。任何人都可以向我解释这些出口之间的区别:

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 个答案:

答案 0 :(得分:2)

所有内容都在modules docs page

<强> Default exports

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

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

Export =

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

CommonJS和AMD通常都有导出对象的概念   其中包含模块的所有导出。

     

他们还支持使用自定义单个替换exports对象   宾语。默认导出旨在替代它   行为;但是,这两者是不相容的。 TypeScript支持   export =模拟传统的CommonJS和AMD工作流程。

     

export =语法指定从中导出的单个对象   模块。这可以是类,接口,命名空间,函数或   枚举。

     

使用export =导入模块时,特定于TypeScript的导入   = require(“module”)必须用于导入模块。

您包含的其他表格:

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

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

相关问题