我正在尝试实现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>
);
}
}
&#13;
当我尝试这个时,我在@上遇到意外的令牌错误。我不知道如何解决这个问题。
我错过了什么?
由于
答案 0 :(得分:4)
Babel 6不再支持ES装饰器语法out of the box,因此您需要安装babel-plugin-transform-decorators-legacy
插件。
npm install --save-dev babel-plugin-transform-decorators-legacy
.babelrc
{
"plugins": [
"transform-decorators-legacy"
]
}
直接在webpack.config.js
{
test: /\.jsx?$/,
loader: 'babel',
query: {
cacheDirectory: true,
plugins: ['transform-decorators-legacy' ]
}
}
答案 1 :(得分:0)
我在这里找到答案:How do I get decorators working with babel & webpack?
感谢@cbll提到装饰者,因为我不知道这是正确的术语。帮助我寻找解决方案