【发布时间】:2017-02-16 05:39:39
【问题描述】:
我想在我的 Angular 1.3 + grunt 堆栈中开始使用 ES6 (2015),而不重构整个现有的 ES5 代码,(或使用其他工具切换 grunt..) 但是当尝试从“旧”控制器中使用新的 ES6 服务时,我遇到了以下错误,
" 无法读取 null 的属性 '1' 在 Function.annotate [as $$annotate] .... "
grunt 中的 babel 配置:
babel: {
options: {
sourceMap: true,
presets: ['es2015']
},
dist: {
files: [{
expand: true,
cwd: '<%= yeoman.app %>',
src: '**/*.es6.js',
dest: '.tmp/app',
ext: '.es5.js'
}]
},
test: {
files: [{
expand: true,
cwd: 'test/spec',
src: '{,*/}*.es6.js',
dest: '.tmp/spec',
ext: '.es5.js'
}]
}
},
服务代码:
class InfoService {
constructor($http) {
this.$http = $http;
}
getInfo() {
console.log('getting');
return this.$http.get('/backend/something/readAll');
}
}
InfoService.$inject = ['$http'];
angular.module('app').service('Info', $http => InfoService($http));
在es5控制器中的使用:
angular.module('app').controller('SomeCtrl',
function ComposerCtrl(Info) {
Info.getInfo();
});
转译后的 ES5 InfoService 是在 .tmp/app 下生成的(我配置了 grunt watch 以在开发时更新更改)所以我想知道我做错了什么..
【问题讨论】:
标签: angularjs ecmascript-6 babeljs