是否可以在 ES6/7 中导出箭头函数? [英] Is it possible to export Arrow functions in ES6/7?

查看:38
本文介绍了是否可以在 ES6/7 中导出箭头函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

export default const hello = () =>console.log("打个招呼")

为什么?

我只能导出命名函数

导出函数hello() {控制台日志(你好")}

是什么原因?

解决方案

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

是的.export 不关心你想导出的值.

<块引用>

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

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

要么做

export default () =>console.log("打个招呼");

const hello = () =>console.log("打个招呼");导出默认你好;

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?

解决方案

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;

这篇关于是否可以在 ES6/7 中导出箭头函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆