Ecmascript 6 grunt babel中的静态关键字抛出错误
我试着在babeljs的标签下转换代码-Ecmascript 6 grunt babel中的静态关键字抛出错误,ecmascript-6,babeljs,Ecmascript 6,Babeljs,我试着在babeljs的标签下转换代码- class aboutController{ constructor(ajaxService){ this.ajaxService = ajaxService; this.printLog(); } printLog(){ this.ajaxService.log(); } static $inject = ["ajaxService"
class aboutController{
constructor(ajaxService){
this.ajaxService = ajaxService;
this.printLog();
}
printLog(){
this.ajaxService.log();
}
static $inject = ["ajaxService"];
}
静态关键字已转换为
{
key: "$inject",
value: ["ajaxService"],
enumerable: true
}
然后我尝试了grunt babel任务来自动化构建。我的GrunFile.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关键字时,构建正在传递。你知道怎么解决这个问题吗。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"
}
}
ES6类语法仅支持方法。您的示例使用ES7建议的类属性。如果您已选中“实验性”选项,则在Babel中启用“尝试” 仅当您专门启用了
es7.classProperties
或广泛启用了所有阶段0转换时,此转换才有效
与ES6兼容的方法是
static get $inject(){
return ["ajaxService"];
}
感谢您指出正确的es6方法。我在选项中将舞台改为0,效果很好。
static get $inject(){
return ["ajaxService"];
}