javascript - Angular2 + Babel + Gulp - 装饰器不会转换为 es5

标签 javascript angular gulp babeljs

Babel 在将装饰器(@Component + @View)转换为 es5 时出错。我是否在我的 gulp 文件中遗漏了一些东西,或者我怎样才能让它工作?

ES2015 文件 (App.es6)

import {Component, View, bootstrap} from 'angular2/angular2';

@Component({
  selector: 'my-app'
})
@View({
  template: '<h1>Hello {{ name }}</h1>'
})
// Component controller
class MyAppComponent {
  name: string;
  constructor() {
    this.name = 'Alice';
  }
}

bootstrap(MyAppComponent);

Gulp 文件

var gulp = require('gulp');
var babel = require('gulp-babel');

gulp.task('default', function () {
    return gulp.src('App.es6')
        .pipe(babel())
        .pipe(gulp.dest('dist'));
});

最佳答案

通天塔 6

我在让装饰器与 Babel 6 一起工作时遇到问题。从今天 (2016-01-17) 开始,我收到一个错误

Decorators are not supported yet in 6.x pending proposal update.

我必须做的解决方案是使用 babel-plugin-transform-decorators-legacy

我必须安装以下 npm 包:

  • babel-plugin-syntax-decorators
  • babel-plugin-transform-decorators-legacy

我的 .babelrc 看起来像

{
  "presets": ["es2015"],
  "plugins": ["transform-decorators-legacy"]
}

关于javascript - Angular2 + Babel + Gulp - 装饰器不会转换为 es5,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32489605/

相关文章:

javascript - jsdoc如何描述返回SAME CLASS变量的静态类方法

javascript - 在 Javascript 中将对象的一部分从数字键转换为数组

javascript - 如何获得 Mongoose 属性(property)的总和?

angular - 获取控件组件内的控件属性

javascript - 点击填写表格?

angular - 由于添加了新成员,如何更新 Angular 的 html 部分中的 ngFor ?

angular - tap() 未在 RXJS 管道中触发

javascript - 如何使用 gulp 更改 href 链接

CSS3 动画停止在 gulp --production 上工作

node.js - 如何创建参数化和可重用的 gulp 任务