为什么这是可行的:
const str = 'stuff';
export {
str
};但不是这样的:
export default {
str: 'stuff'
};我想像下面这样导入:
import { str } from 'myLib';我想直接在导出中赋值,而不需要事先创建变量。
同样,当我尝试的时候:
export {
str: 'stuff'
};我得到了错误:
SyntaxError: /home/karlm/dev/project/ex.js: Unexpected token, expected , (41:5)
39 |
40 | export {
> 41 | str: 'stuff'
| ^
42 | };
43 | 发布于 2016-12-22 21:08:25
在ES6中有两种样式的导出--命名导出和默认导出。使用如下语法导出已命名的导出:
export const str = 'stuff';
// or
const str = 'stuff';
export { str };默认导出如下所示:
const obj = { str: 'stuff' };
export default obj;
// or
export default {
str: 'stuff'
};当您导入时,差异就会显现出来。对于第一个,您需要包括大括号:
import { str } from 'myModule'; // 'stuff', from the first example如果没有大括号,它将导入默认导出:
import myModule from 'myModule'; // {str: 'stuff'}, from the second example发布于 2016-12-22 21:02:31
export statement的主要用途是用于从给定的文件(或模块)中导出函数、对象或原语。
但是您需要一个标识符才能导出(这样它就可以通过另一个脚本中的import导入)。
您可以简单地这样做:
export const obj = {
str: 'stuff'
};在import期间,您将能够使用相同的名称obj来引用相应的值。
并像这样导入它:
import { obj } from 'myLib';https://stackoverflow.com/questions/41283733
复制相似问题