解析错误:意外的令牌const [英] Parsing error: Unexpected token const

查看:157
本文介绍了解析错误:意外的令牌const的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前正在设置一个流星应用程序,并且正在使用eslint和babel,但是以下代码段出现以下错误:

I am currently setting up a meteor application and I am using eslint and babel, but I get the following error for the following code snippet:

const Navigation = props => (
  const classes = props.classes;

  return (
    <div className={classes.root}>
    </div>
  )
);

错误:

2:4 - Parsing error: Unexpected token const

我已经重新我eslint配置此处. 我的.babelrc配置如下:

I have recreated my eslint config here. My .babelrc config is the following:

{
  "presets": ["env", "react"]
}

推荐答案

这是因为您使用的是

It's because you are using the concise body of an arrow function and that expects the expression inside () and not a statement. To use a statement you need to use block body by using {} instead of ().

赞:

const Navigation = props => {
  const classes = props.classes;

  return (
    <div className={classes.root}>
    </div>
  )
};

根据 MDN文档 :

As per MDN Doc:

箭头功能可以具有简洁主体"或通常的块" 身体".

Arrow functions can have either a "concise body" or the usual "block body".

在简洁的正文中,仅需要一个表达式,而隐式 返回附件.在块体中,必须使用显式返回 声明.

In a concise body, only an expression is needed, and an implicit return is attached. In a block body, you must use an explicit return statement.

这篇关于解析错误:意外的令牌const的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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