是否可以在ES6/7中导出Arrow函数

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

本文关键字:Arrow 函数 ES6 是否      更新时间:2024-02-13

下面的导出语句给出了一个语法错误

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

为什么?

我只能导出命名函数

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

原因是什么?

是否可以在ES6/7中导出Arrow函数?

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

下面的导出语句出现语法错误。。。为什么?

您不能有一个默认导出,并且给它一个名称("默认"已经是导出的名称)。

要么做

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

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

如果您不想要默认导出,您可以简单地使用以下语法导出命名函数:

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

试试这个

导出默认值()=>console.log("说你好");

导出常量hello=()=>console.log("说你好")