簡體   English   中英

grunt-babel中的靜態關鍵字拋出錯誤

[英]static keyword throwing error in grunt-babel

我試圖在babeljs的試用標簽中轉碼以下代碼-

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

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

      static $inject = ["ajaxService"];
    }

將static關鍵字轉譯為

{
  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"]);
}

但是現在static關鍵字給出了錯誤,當我刪除static關鍵字時,構建正在通過。 任何想法如何解決此問題。 咕bab巴貝過時了嗎?

這是我的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類語法僅支持方法。 您的示例使用ES7建議的類屬性。 如果您選中了“實驗性”,則在Babel中的“試用”上啟用它們。

 static $inject = ["ajaxService"];

僅在您專門啟用es7.classProperties或廣泛啟用所有階段0轉換的情況下才有效。

與ES6兼容的方法是

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM