[英]Decorators with babel, unexpected token
I'm trying to use decorators on classes in React, using babelify. 我正在尝试使用babelify在React的类上使用装饰器。 I have the 'es7.decorators' option applied in babel, but I keep getting an 'unexpected token' error when it encounters the '@' character.
我在babel中应用了“ es7.decorators”选项,但是当遇到“ @”字符时,我一直收到“意外令牌”错误。
Anyone have any ideas? 有人有想法么? A simple example is below.
下面是一个简单的示例。
Decorator: 装饰器:
export default function(Component) {
return class extends Component {
constructor() {...}
}
}
Class: 类:
import myDecorator from 'decorator';
@myDecorator
class MyClass{...}
I'm using babelify (Browserify transform for Babel): 我正在使用babelify (针对Babel的浏览器转换):
browserify().transform(babelify.configure({
optional: ['es7.decorators']
})
感谢@LeonidBeschastny提到.babelrc
文件,使用配置文件装饰器可以正常工作,使用babelify自述文件中描述的设置由于任何原因均不起作用(不确定我的设置是否正确)。
In case anyone else ran into this problem, I was having the same problem. 万一其他人遇到这个问题,我也有同样的问题。
I think there were breaking changes outlined here: http://babeljs.io/blog/2015/03/31/5.0.0/#babelrc 我认为这里概述了一些重大更改: http : //babeljs.io/blog/2015/03/31/5.0.0/#babelrc
All I needed to do was add { "stage": 1 } to my babelrc, which tells babel to compile with the experimental features, one of which is the es7 decorator. 我需要做的就是在babelrc中添加{“ stage”:1},这告诉babel使用实验功能进行编译,其中之一是es7装饰器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.