文件:SafeString.js

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

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

export default SafeString;

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


当前回答

导出默认值用于从文件中仅导出一个值,该文件可以是类、函数或对象。默认导出可以使用任何名称导入。

//file functions.js

export default function subtract(x, y) {
  return x - y;
}

//importing subtract in another file in the same directory
import myDefault from "./functions.js";

在导入的文件中,减法函数可以称为myDefault。

导出默认值还会创建一个回退值,这意味着如果您尝试导入命名导出中不存在的函数、类或对象。将提供默认导出提供的回退值。

有关详细说明,请访问https://developer.mozilla.org/en-US/docs/web/javascript/reference/statements/export

其他回答

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

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

阅读更多信息

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

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


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

如本MDN页所述

有两种不同的导出类型,命名和默认。你可以每个模块有多个命名导出,但只有一个默认值export[…]命名导出对于导出多个值非常有用。在期间导入时,必须使用相应的对象。但可以使用任何名称导入默认导出

例如:

let myVar; export default myVar = 123; // in file my-module.js

import myExportedVar from './my-module' //  we have the freedom to use 'import myExportedVar' instead of 'import myVar' because myVar was defined as default export

console.log(myExportedVar);        // will log 123

JavaScript中的“导出默认值”是什么?在默认导出中,导入的命名是完全独立的,我们可以使用任何我们喜欢的名称。

我将用一个简单的例子来说明这一行。

假设我们有三个模块和一个index.html文件:

模块.js模块2.js模块3.js索引html

文件modul.js

export function hello() {
    console.log("Modul: Saying hello!");
}

export let variable = 123;

文件modul2.js

export function hello2() {
    console.log("Module2: Saying hello for the second time!");
}

export let variable2 = 456;

模块3.js

export default function hello3() {
    console.log("Module3: Saying hello for the third time!");
}

文件index.html

<script type="module">
import * as mod from './modul.js';
import {hello2, variable2} from './modul2.js';
import blabla from './modul3.js'; // ! Here is the important stuff - we name the variable for the module as we like

mod.hello();
console.log("Module: " + mod.variable);

hello2();
console.log("Module2: " + variable2);

blabla();
</script>

输出为:

modul.js:2:10   -> Modul: Saying hello!
index.html:7:9  -> Module: 123
modul2.js:2:10  -> Module2: Saying hello for the second time!
index.html:10:9 -> Module2: 456
modul3.js:2:10  -> Module3: Saying hello for the third time!

因此,更长的解释是:

如果要为模块导出单个对象,则使用“导出默认值”。

因此,重要的是“从'./module3.js'导入blabla”-我们可以改为:

“从'./modul3.js导入pamelanderson”,然后导入pameladerson();。当我们使用“导出默认值”时,这将很好地工作,基本上就是这样-它允许我们在默认值时随意命名它。


P.S.:如果你想测试这个示例-首先创建文件,然后在浏览器中允许CORS->如果你使用Firefox,请在浏览器的URL中键入:about:config->搜索“privacy.file_unique_origin”->将其更改为“false”->打开index.html->按F12打开控制台并查看输出->享受并不要忘记将CORS设置返回默认值。

P.S.2:对不起,变量命名太愚蠢了

更多信息请参见link2medium和link2mdn。

导出默认值用于从文件中仅导出一个值,该文件可以是类、函数或对象。默认导出可以使用任何名称导入。

//file functions.js

export default function subtract(x, y) {
  return x - y;
}

//importing subtract in another file in the same directory
import myDefault from "./functions.js";

在导入的文件中,减法函数可以称为myDefault。

导出默认值还会创建一个回退值,这意味着如果您尝试导入命名导出中不存在的函数、类或对象。将提供默认导出提供的回退值。

有关详细说明,请访问https://developer.mozilla.org/en-US/docs/web/javascript/reference/statements/export