Decorators with babel, unexpected token

I'm trying to use decorators on classes in React, using babelify. I have the 'es7.decorators' option applied in babel, but I keep getting an 'unexpected token' error when it encounters the '@' character.

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):

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

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.

链接地址: http://www.djcxy.com/p/32966.html

上一篇: gulp:browserify / babelify构建脚本,意外的令牌

下一篇: 装饰师与巴贝尔,意想不到的令牌