简体   繁体   English

grunt-babel中的静态关键字抛出错误

[英]static keyword throwing error in grunt-babel

I tried to trans-pile below code in try it out tab of babeljs - 我试图在babeljs的试用标签中转码以下代码-

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

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

      static $inject = ["ajaxService"];
    }

The static keyword got trans-piled to 将static关键字转译为

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

I then tried out grunt-babel task to automate the build. 然后,我尝试了grunt-babel任务来自动化构建。 My gruntfile.js looks like this - 我的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"]);
}

But now the static keyword is giving error, when I remove the static keyword, the build is passing. 但是现在static关键字给出了错误,当我删除static关键字时,构建正在通过。 Any idea how to fix this. 任何想法如何解决此问题。 Is grunt-babel outdated? 咕bab巴贝过时了吗?

Here is how my packahe.json looks like - 这是我的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"
}
}

ES6 class syntax only supports methods. ES6类语法仅支持方法。 Your example uses ES7-proposed class properties. 您的示例使用ES7建议的类属性。 They are enabled in Babel on "Try it out" if you have "Experimental" checked. 如果您选中了“实验性”,则在Babel中的“试用”上启用它们。

 static $inject = ["ajaxService"];

will only work if you specifically enable es7.classProperties or broadly enabled all stage 0 transforms. 仅在您专门启用es7.classProperties或广泛启用所有阶段0转换的情况下才有效。

The ES6-compatible approach would be 与ES6兼容的方法是

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM