简体   繁体   English

React问题-@是意外令牌

[英]React issue - @ is an unexpected token

I'm trying to implement react-keydown which can be found here: https://github.com/glortho/react-keydown using the following code example: 我正在尝试实现react-keydown,可以在下面的代码示例中找到它: https : //github.com/glortho/react-keydown

 import React from 'react'; import keydown, { Keys } from 'react-keydown'; class MethodDecoratorExample extends React.Component { constructor( props ) { super( props ); this.state = { hello: false }; } @keydown( 'enter' ) toggleHello() { this.setState( { hello: !this.state.hello } ); } render() { return ( <div> <h3>Method Decorator Example</h3> <div>Press the <strong>enter</strong> key to toggle hello.</div> { this.state.hello && <h1>Enter is key code {Keys.enter}!</h1> } <div>And click again outside box to see scoping.</div> </div> ); } } 

When I try this I get an unexpected token error on the @. 尝试此操作时,我在@上收到意外的令牌错误。 I have no clue how to solve this. 我不知道如何解决这个问题。

What am I missing? 我想念什么?

Thanks 谢谢

Babel 6 no longer supports ES decorator syntax out of the box so you need to install the babel-plugin-transform-decorators-legacy plugin. Babel 6不再提供开箱即用的 ES装饰器语法因此您需要安装babel-plugin-transform-decorators-legacy插件。

Install babel-plugin-transform-decorators-legacy 安装babel-plugin-transform-decorators-legacy

npm install --save-dev babel-plugin-transform-decorators-legacy

Add the plugin in your .babelrc 在您的.babelrc添加插件

{
  "plugins": [
    "transform-decorators-legacy"
  ]
}

or 要么

add the loader directly in your webpack.config.js 将加载程序直接添加到webpack.config.js

{
  test: /\.jsx?$/,
  loader: 'babel',
  query: {
    cacheDirectory: true,
    plugins: ['transform-decorators-legacy' ]
  }
}

I found the answer here: How do I get decorators working with babel & webpack? 我在这里找到了答案: 如何使装饰器与babel和webpack一起使用?

Thanks to @cbll for mentioning decorators, because I did not know that that is the correct term. 感谢@cbll提到装饰器,因为我不知道那是正确的术语。 Helped me with searching for solutions 帮助我寻找解决方案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM