【问题标题】:What is "export default" in JavaScript?什么是 JavaScript 中的“导出默认值”?
【发布时间】:2020-12-06 23:34:28
【问题描述】:

文件:SafeString.js

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

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

export default SafeString;

我以前从未见过export defaultexport default 有没有更容易理解的等效内容?

【问题讨论】:

标签: javascript node.js ecmascript-6


【解决方案1】:

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

如果模块定义了默认导出:

// foo.js
export default function() { console.log("hello!") }

然后您可以通过省略花括号来导入该默认导出:

import foo from "foo";
foo(); // hello!

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

【讨论】:

  • @GeenHenk 我想这是意料之中的,因为 ES6 仍然是草案。我提供了更新的链接和免责声明。
  • 我看不出 export default function(){}export = function(){} 有何不同....
  • 如果是 export const Foo = () => {} 然后在文件末尾 export default Foo 我在一堆反应示例中看到了这一点。这两个出口是怎么回事?
  • 很高兴看到一个带有默认和命名导出的示例。换句话说,这样的导出将满足import foo, { bar, baz } from './foo';
  • 感谢回答,但第二个例子中 foo 的用法有点含糊;什么是 foo 以及您如何命名第一个文件;你怎么能做到import foo from "foo"?是否有一个包含 foo 的对象,因为在第一个示例中,您导出的函数未命名。 @p.s.w.g
【解决方案2】:

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

This page 详细描述了export default 以及我认为非常有用的模块的其他详细信息。

【讨论】:

  • 如果您愿意,可以同时使用默认导出和命名导出。
  • @Greg gum 该页面已过时。它正在重定向到exploringjs.com/es6/ch_modules.html
  • @rajakvk,没错,但原始页面为入门者提供了更多背景信息。
  • 这个答案比接受的要好,因为它解释了default 的意思,对我来说问题是关于这个词。
  • @DariuszSikorski 接受的答案解释了default 的含义,即可以在不使用大括号的情况下导入默认导出。这个答案实际上是非常错误的,因为它说您只能在文件中只有一个导出时使用default,这根本不是真的。您可以在同一个文件中有多个导出,但当然只有其中一个可以设置为default 一个。
【解决方案3】:

export default 用于从脚本文件中导出单个类、函数或原语。

导出也可以写成

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

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

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

app.js中说,可以

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

关于出口的一点点

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

Utiliites.js

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

这个可以导入使用

App.js

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

或者

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

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

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

并用作 App.js

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

【讨论】:

  • import {cube} from ... vs import cube from ... ?已经很简单了,那还有什么意义呢?
【解决方案4】:

正如MDN page所解释的那样

有两种不同类型的导出,命名和默认。你可以 每个模块有多个命名导出,但只有一个默认值 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

【讨论】:

  • 如果决定使用现有名称 myVar 作为默认名称怎么办?
【解决方案5】:

在我看来,默认导出的重要是它可以任何名称导入!

如果有文件,foo.js,默认导出:

export default function foo(){}

可以在 bar.js 中使用:

import bar from 'foo'
import Bar from 'foo' // Or ANY other name you wish to assign to this import

【讨论】:

    【解决方案6】:

    什么是 JavaScript 中的“导出默认值”?

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

    我会用一个简单的例子来说明这条线。

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

    • modul.js
    • modul2.js
    • modul3.js
    • index.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!
    

    所以较长的解释是

    如果您想为模块导出单个事物,则使用“导出默认值”。

    所以重要的是“从'./modul3.js'导入blabla”——我们可以改为:

    “从'./modul3.js”导入pamelanderson,然后pamelanderson();。当我们使用 'export default' 时,这将正常工作,基本上就是这样 - 它允许我们在默认情况下随意命名它


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

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

    更多信息在link2mediumlink2mdn

    【讨论】:

      【解决方案7】:

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

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

      Read more

      【讨论】:

        【解决方案8】:

        有两种不同类型的导出,命名默认。每个模块可以有多个命名导出,但只有一个默认导出。每种类型对应于上述一种。 Source: 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.
        

        【讨论】:

          【解决方案9】:

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

          //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。

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

          详细解释可以在https://developer.mozilla.org/en-US/docs/web/javascript/reference/statements/export找到

          【讨论】:

            【解决方案10】:

            export和export default有什么区别?

            命名导出对于导出多个值很有用。在导入过程中,可以使用相同的名称来引用相应的值。

            关于默认导出,每个模块只有一个默认导出。默认导出可以是函数、类、对象或其他任何东西。

            【讨论】:

              猜你喜欢
              • 2019-11-23
              • 1970-01-01
              • 1970-01-01
              • 2022-11-15
              • 2016-07-25
              • 2015-12-13
              • 2017-07-09
              相关资源
              最近更新 更多