【问题标题】:static keyword throwing error in grunt-babelgrunt-babel 中的静态关键字抛出错误
【发布时间】:2015-12-01 22:40:41
【问题描述】:

我试图在 babeljs 的try it out 标签中转换下面的代码 -

    class aboutController{
      constructor(ajaxService){
        this.ajaxService = ajaxService;
        this.printLog();
      }

      printLog(){
        this.ajaxService.log();
      }

      static $inject = ["ajaxService"];
    }

静态关键字被转换成

{
  key: "$inject",
  value: ["ajaxService"],
  enumerable: true
}

然后我尝试了 grunt-babel 任务来自动化构建。我的 gruntfile.js 看起来像这样 -

module.exports = function(grunt){
grunt.initConfig({
    pkg: grunt.file.readJSON('package.json'),
    babel: {
        options: {
            sourceMap: true,
            highlightCode: true
        },
        es6: {
            files: [
                {
                    expand: true,
                    src: ['components/**/*.es6'],
                    ext: '.js'
                }
            ]
        }
    },
    watch: {
        scripts: {
            files: ['components/**/*.es6'],
            tasks:['babel']
        }
    }
});

require("load-grunt-tasks")(grunt);
grunt.registerTask("default", ["babel", "watch"]);
}

但是现在静态关键字给出错误,当我删除静态关键字时,构建正在通过。任何想法如何解决这个问题。 grunt-babel 过时了吗?

这是我的 packahe.json 的样子 -

{
"name": "babeles6",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"devDependencies": {
    "grunt": "~0.4.2",
    "grunt-babel": "^5.0.1",
    "grunt-contrib-watch": "^0.6.1",
    "load-grunt-tasks": "^3.2.0"
}
}

【问题讨论】:

    标签: ecmascript-6 babeljs


    【解决方案1】:

    ES6 类语法只支持方法。您的示例使用 ES7 提出的类属性。如果您选中了“实验”,它们会在 Babel 中的“试用”中启用。

     static $inject = ["ajaxService"];
    

    仅当您专门启用 es7.classProperties 或广泛启用所有阶段 0 转换时才有效。

    兼容 ES6 的方法是

    static get $inject(){
        return ["ajaxService"];
    }
    

    【讨论】:

    • 感谢您指出正确的 es6 方法。我在选项中将阶段更改为 0,它就像魅力一样。
    猜你喜欢
    • 2017-11-16
    • 2012-06-09
    • 1970-01-01
    • 2021-04-09
    • 2020-04-06
    • 1970-01-01
    • 1970-01-01
    • 2018-12-09
    • 1970-01-01
    相关资源
    最近更新 更多