是否可以导出ES6 / 7中的箭头功能?

是否可以导出ES6 / 7中的箭头功能?

问题描述:

下面的导出语句提供语法错误

The export statement below gives a syntax error

export default const hello = () => console.log("say hello")

为什么?

我只能导出命名函数

export function hello() {
  console.log("hello")
}

原因是什么?


是否可以导出ES6 / 7中的箭头功能?

Is it possible to export Arrow functions in ES6/7?

是的。 export 不关心您要导出的值。

Yes. export doesn't care about the value you want to export.


导出下面的语句给出了一个语法错误...为什么?

The export statement below gives a syntax error ... why?

你不能有默认导出给它一个名称(默认已经是导出的名称)。

You cannot have a default export and give it a name ("default" is already the name of the export).



Either do

export default () => console.log("say hello");

const hello = () => console.log("say hello");
export default hello;