2015-09-06 27 views
2

我試圖反式樁下面的代碼babeljs的try it out標籤 -static關鍵字在咕嚕 - 巴貝爾拋出錯誤

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

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

     static $inject = ["ajaxService"]; 
    } 

static關鍵字得到的反式堆到

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

然後我試着out 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關鍵字,構建在流逝。任何想法如何解決這個問題。咕嚕咕嚕是否過時?

這裏是我的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" 
} 
} 

回答

3

ES6類語法僅支持方法。你的例子使用ES7建議的類屬性。如果您選中「實驗」,則可以在「試用」中啓用Babel。

static $inject = ["ajaxService"]; 

,如果你明確地使es7.classProperties或寬啓用的所有階段0變換纔會工作。

的ES6兼容的做法是

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

感謝您指出正確的ES6 apporach。我在選項中將舞臺改爲0,並且像魅力一樣工作。 –