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

泪湿孤枕 提交于 2019-12-17 15:53:10

问题


The export statement below gives a syntax error

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

why ?

I'm only able to export named functions

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

What is the reason?


回答1:


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

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");

or

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



回答2:


If you don't want a default export, you can simply export a named function with this syntax:

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


来源:https://stackoverflow.com/questions/36458697/is-it-possible-to-export-arrow-functions-in-es6-7

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!