我正在使用ES6 codestyle并实现这样的示例模块:
import angular from 'angular';
import { IndicatorSelectorComponent } from './indicatorSelector.component';
import './indicatorSelector.css';
export const IndicatorSelectorModule = angular
.module('indicatorSelector', [])
.component('indicatorSelector', IndicatorSelectorComponent)
.name;
使用此组件的代码:
import templateUrl from './indicatorSelector.html';
export const IndicatorSelectorComponent = {
templateUrl,
controller: class IndicatorSelectorComponent {
contructor($http) {
'ngInject';
this.$http = $http;
}
$onInit() {
console.log(this.$http);
}
}
}
console.log(此。$ http)返回undefined。我怀疑问题出在我的webpack代码和ng-annotate上,但我可以看到生成的bundle包含了注释:
var IndicatorSelectorComponent = exports.IndicatorSelectorComponent = {
templateUrl: _indicatorSelector2.default,
controller: function () {
function IndicatorSelectorComponent() {
_classCallCheck(this, IndicatorSelectorComponent);
}
_createClass(IndicatorSelectorComponent, [{
key: 'contructor',
value: ["$http", function contructor($http) { // <==== HERE!
'ngInject';
this.$http = $http;
}]
}, {
key: '$onInit',
value: function $onInit() {
console.log(this.$http);
}
}]);
return IndicatorSelectorComponent;
}()
};
但它仍然没有用。我试图在模块代码上检查console.log(IndicatorSelectorComponent);
我可以看到$ inject是空的。我不知道还能做什么。我对此表示感谢。
Object
controller :function IndicatorSelectorComponent()
$inject :Array[0]
arguments : (...)
caller : (...)
length : 0
name : "IndicatorSelectorComponent"
prototype : Object
__proto__ : function ()
[[FunctionLocation]] : indicatorSelector.component.js:5
[[Scopes]] : Scopes[3]
templateUrl : "C:/Projetos/IndicadoresPCM/client/src/app/components/goals/indicatorSelector/indicatorSelector.html"
__proto__ : Object
答案 0 :(得分:0)
如果使用这种方式,它将起作用。
import templateUrl from './indicatorSelector.html';
export const IndicatorSelectorComponent = {
templateUrl,
controller: function ($http) {
"ngInject";
this.$onInit = function () {
console.log($http);
};
}
}
但是,让它与类表达式一起工作会非常有趣......