Es6 模块(Module)导入、导出几种常用的方式

声明: 来源是看到一篇老外写的文章,整理翻译成中文

es6的模块导出大致有导出单个名称、default默认值、还有导出多个list三种形式

三种方式导出、导入大致如下:

// Name Export | Name Import
export const name = 'value'
import { name } from '...'

// Default Export | Default Import
export default 'value'
import anyName from '...'

// Rename Export | NameImport
export { name as newName }
import { newName } from '...'

// Name + Default | Import All
export const name = 'value'
export default 'value'
import * as anyName from '...'

// Export List + Rename | Import List + Rename
export {
  name1,
  name2 as newName2
}
import {
  name1 as newName1,
  newName2
} from '...'

以下依次介绍:

a. Name 按名称导出

export const name = 'value';
import { name } from 'some-path/file';

console.log(name); // 'value'

常见错误写法:

❌ What did I say, no name, no export! (就像我说的,没有名称,就不要导出)

export 'value'

import { } // 👈 see I don't even know what to put here...give me a name!

导出时没有定义名称,在导入的时候,都不知道放什么名称

b.Default 默认导出

With a default export, you don’t need any name. Because you can name it whatever you want 👏 (用默认导出,你不需要提供名称,因为你可以自己想怎么定义都行)


export default 'value'


import anyName from 'some-path/file';

console.log(anyName); // 'value'

常见错误:
❌ No Variable Declaration with Default (用默认导出的时候,不需要变量声明)


export default const name = 'value'; // don't try to give me a name!

可以混用 默认导出default + 名称导出name

You can absolutely combine default and name export in one file 🤝 (当然可以在一个文件内上面两种方式混用)


export const name = 'value';
export default 'value'


import anyName, { name } from 'some-path/file';

c. Export List 按组导出


const name1 = 'value1';
const name2 = 'value2';

export {
  name1,
  name2
}


import {
  name1,
  name2
} from 'some-path/file'

console.log(
  name1, // 'value1'
  name2, // 'value2'
)

常见错误:

One important thing to note is that these lists are NOT objects. Yes, I know it looks like it. But it isn’t. I made this confusion when I first learned modules. It’s not an object, it’s an export list! (很重要的一点,这个导出的,并不是一个对象,尽管看起来很像。我第一次接触的时候,也很困惑。但它的确导出的不是一个对象类型,而是一个列表)

// ❌ Export list ≠ Object
export {
  name: 'name'
}

重命名导出 Renaming Export

Not happy with the export name. No problem, you can rename it using the as keyword.


const name = 'value'

export {
  name as newName
}


import { newName } from 'some-path/file'

console.log(newName); // 'value'

// Original name is not accessible
// 原来的名称,不可用了
console.log(name); // ❌ undefined

❌ Can not combine inline export with export list (不要在按名称导出后又按组导出它)


export const name = 'value';

// You're already exporting name ☝️, don't export me again
export {
  name
}

重命名导入 Renaming Import


const name1 = 'value1';
const name2 = 'value2';

export {
  name1,
  name2 as newName2
}


import {
  name1 as newName1,
  newName2
} from '...'

console.log(newName1); // 'value1'
console.log(newName2); // 'value2'

❌
name1; // undefined
name2; // undefined

···

## 导入所有 Import All

```js

export const name = 'value';

export default 'defaultValue';


import * as anyName from 'some-path/file';

console.log(anyName.name); // 'value'
console.log(anyName.default); // 'defaultValue'

  • 5
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: ES6是ECMAScript 6的简称,也称为ES2015。在ES6中,我们可以使用新的语法来导出模块和命名变量。 1. 导出模块ES6中,我们可以使用关键字`export`将模块导出导出模块可以被其他模块引用。导出方式有以下几种: - 默认导出 默认导出可以只导出一个值,使用`export default`关键字。被导出的值可以是任意类型,包括函数、类、对象等。例如: ``` // module.js export default function add(a, b) { return a + b; } // app.js import add from './module.js'; console.log(add(1, 2)); // 输出 3 ``` - 命名导出 命名导出可以导出多个值,使用`export`关键字。被导出的值必须使用名称来标识,例如: ``` // module.js export const name = 'Tom'; export const age = 18; // app.js import { name, age } from './module.js'; console.log(name, age); // 输出 Tom 18 ``` - 统一导出 统一导出可以将多个模块导出成一个模块,使用`export`关键字。例如: ``` // module1.js export function add(a, b) { return a + b; } // module2.js export function sub(a, b) { return a - b; } // module.js export * from './module1.js'; export * from './module2.js'; // app.js import { add, sub } from './module.js'; console.log(add(1, 2)); // 输出 3 console.log(sub(3, 2)); // 输出 1 ``` 2. 命名 在ES6中,我们可以使用`const`和`let`关键字来声明变量。变量的命名规则与其他语言相同,由字母、数字和下划线组成,但不能以数字开头。例如: ``` const name = 'Tom'; let age = 18; const PI = 3.1415; ``` 值得注意的是,ES6中还新增了`模板字符串`和`箭头函数`等语法,可以更方便地书写代码。例如: ``` const name = 'Tom'; const age = 18; console.log(`My name is ${name}, I'm ${age} years old.`); const add = (a, b) => a + b; console.log(add(1, 2)); // 输出 3 ``` 以上就是ES6导出和命名的详细解释。 ### 回答2: ES6导出和命名是指在ES6模块系统中,如何将代码从一个模块导出并在另一个模块中使用。ES6导出和命名语法提供了灵活和可重用的模块化功能。 ES6导出可以使用两种方式:默认导出和命名导出。 默认导出(Default Exports)是指在一个模块中默认导出一个值。默认导出可以是任何类型的值,例如变量、函数、对象、类等。在导入时,可以选择使用任意名称来引用默认导出的值。 命名导出(Named Exports)是指在一个模块导出多个值,并使用命名来引用这些值。命名导出必须使用“export”关键字定义,可以导出变量、函数、对象、类等。在导入时,必须使用相应的名称来引用导出的值。 例如,在一个模块中,可以使用默认导出一个函数: export default function add(a, b) { return a + b; } 在另一个模块中,可以使用任意名称来引用这个默认导出的函数: import customAdd from './mathFunctions'; console.log(customAdd(3, 4)); // 输出:7 在同一个模块中,也可以使用命名导出多个值: export function multiply(a, b) { return a * b; } export const PI = 3.14; 在另一个模块中,必须使用相应的名称来引用这些命名导出的值: import { multiply, PI } from './mathFunctions'; console.log(multiply(3, 4)); // 输出:12 console.log(PI); // 输出:3.14 通过ES6导出和命名功能,可以轻松地将代码整理成可维护和可重用的模块,提高代码的可读性和可维护性。它也为开发者提供了更好的灵活性,可以根据需要选择默认导出或命名导出。 ### 回答3: ES6模块化语法中,提供了两种导出方式:默认导出(export default)和命名导出(export)。 默认导出允许我们将一个模块中的默认值导出,一个模块只能有一个默认导出。当我们在导入模块时,可以选择是否使用默认导出的值。默认导出在一个模块中是独立的,不需要使用花括号进行导入。在导出时,可以直接使用关键字"default"来标记。 例如,在一个名为moduleA的模块中,我们可以这样默认导出一个函数: export default function add(a, b) { return a + b; } 在另一个模块中,我们可以这样导入并使用默认导出的函数: import add from './moduleA'; // 直接导入默认值,不需要使用花括号 console.log(add(1, 2)); // 输出:3 命名导出是指将模块中的值,通过指定名称进行导出,可以导出多个变量、函数或常量。在导入时,需要使用相同的名称进行引用。 例如,在模块中我们可以这样命名导出多个变量: export const name = 'John'; export const age = 25; 在另一个模块中,我们可以这样导入并使用命名导出的变量: import { name, age } from './moduleB'; // 使用花括号导入指定名称的变量 console.log(name, age); // 输出:John 25 除了导出变量之外,还可以导出函数或常量等,都遵循相同的导出规则。 总结起来,ES6模块化语法中的导出方式包括默认导出和命名导出,适用于不同的需求。默认导出用于导出一个模块的默认值,并通过关键字"default"进行标记,而命名导出用于导出多个变量、函数或常量,并需要在导入时使用相同的名称进行引用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值