文件:SafeString.js

// Build out our basic SafeString type
function SafeString(string) {
  this.string = string;
}

SafeString.prototype.toString = function() {
  return "" + this.string;
};

export default SafeString;

我以前从未见过导出默认。对于出口默认值,有什么更容易理解的等效内容吗?


当前回答

ES6中引入的一个好特性是javascript模块,它可以高效地在不同的.js文件之间导出和导入变量、函数和类。

我们有两种不同的导出方式:命名导出和默认导出。要正确理解默认导出,我们必须首先很好地理解命名导出。

命名导出

在本例中,在源文件中,我们导出具有特定名称的所需变量、函数或类。语法如下:

// file: source.js
export const myVariable = /* … */
export function myFunction() { /* … */ }
export class myClass { /* … */ }

现在,要访问目标文件中的上述项,必须按如下方式导入它们:

// file: target.js (in the same directory as the source.js file) 
import { myVariable } from "./source.js"
import { myFunction } from "./source.js"
import { myClass } from "./source.js"

现在是时候进入主要问题“默认导出到底是什么”了?

默认导出

除了按名称导出它们(命名为exports)的情况外,还有一个类似的功能,称为默认导出,在每个.js文件中只能使用一次。请参阅以下示例,并将其与之前的source.js文件进行比较:

// file: source.js
export default function myNewFunction() { /* … */ }
export const myVariable = /* … */
export function myFunction() { /* … */ }
export class myClass { /* … */ }

事实上,每个.js文件可以有“多个命名导出”和“只有一个默认导出”,其中myNewFunction作为默认导出。这样,在目标文件中导入时,javascript会了解默认导出的项目。

“导出为默认值”的项目(myNewFunction)将导入target.js文件,如下所示:

// file: target.js (in the same directory as the source.js file)
import anyName from "./source.js"

仔细看看这些区别!这里,我们在导入后没有{}号,我们使用了源文件中没有的自定义名称。这里anyName表示myNewFunction。

这表明,我们可以为导入时“导出为默认值”的项目指定“任何所需名称”,只要指向源文件的“路径”,JavaScript就会找到该文件并将其导入。

一些重要注意事项:

与命名导出不同,在默认导出中,我们不需要导出命名项目,我们也可以导出“未命名”项目。他们为什么要在ES6中添加默认导出功能!?对于能力导出“未命名项”(匿名函数和类)以及表达式,甚至对象文本。

其他回答

导出默认值用于导出单个类、函数或原语。

导出默认函数(){}可以在函数没有名称时使用。一个文件中只能有一个默认导出。

阅读更多信息

导出默认值用于从脚本文件导出单个类、函数或原语。

导出也可以写成

export default function SafeString(string) {
  this.string = string;
}

SafeString.prototype.toString = function() {
  return "" + this.string;
};

这用于在另一个脚本文件中导入此函数

在app.js中说,你可以

import SafeString from './handlebars/safe-string';

关于出口

顾名思义,它用于从脚本文件或模块导出函数、对象、类或表达式

实用程序.js

export function cube(x) {
  return x * x * x;
}
export const foo = Math.PI + Math.SQRT2;

这可以导入并用作

应用程序.js

import { cube, foo } from 'Utilities';
console.log(cube(3)); // 27
console.log(foo);    // 4.555806215962888

Or

import * as utilities from 'Utilities';
console.log(utilities.cube(3)); // 27
console.log(utilities.foo);    // 4.555806215962888

当使用导出默认值时,这要简单得多。脚本文件只导出一件事。立方体.js

export default function cube(x) {
  return x * x * x;
};

并用作应用程序.js

import Cube from 'cube';
console.log(Cube(3)); // 27

有两种不同的导出类型,命名和默认。每个模块可以有多个命名导出,但只有一个默认导出。每种类型都对应于上述类型之一。来源:MDN

命名导出

export class NamedExport1 { }

export class NamedExport2 { }

// Import class
import { NamedExport1 } from 'path-to-file'
import { NamedExport2 } from 'path-to-file'

// OR you can import all at once
import * as namedExports from 'path-to-file'

默认导出

export default class DefaultExport1 { }

// Import class
import DefaultExport1 from 'path-to-file' // No curly braces - {}

//可以为默认导入使用其他名称

import Foo from 'path-to-file' // This will assign any default export to Foo.

导出默认函数(){}可以在函数没有名称时使用。一个文件中只能有一个默认导出。另一种方法是命名导出。

本页详细介绍了导出默认值以及我发现非常有用的模块的其他详细信息。

它是ES6模块系统的一部分,如本文所述。该文档中还有一个有用的示例:

如果模块定义了默认导出://foo.js导出默认函数(){console.log(“hello!”)}然后可以通过省略大括号来导入默认导出:从“foo”导入foo;foo();//你好


更新:截至2015年6月,模块系统在§15.2中定义,尤其是导出语法在ECMAScript 2015规范的§15.2.3中定义。