我已经通过npm下载了此模块https://github.com/skeymeulen/swangular,并希望将其与TypeScript一起使用。现在我正在努力包括依赖关系的正确方法。就像GitHub上的READ.me告诉我,我正在我的应用程序中注入模块:
var app = angular.module("app", ['ui.bootstrap', 'swangular']);
我的控制器如下所示:
class SimpleController {
static readonly $inject = ["$scope", "$http", "$uibModal", "swangular", ProjectsService.serviceName, AttributesService.serviceName];
constructor(
private $scope: ng.IScope,
private $http: ng.IHttpService,
private $uibModal: ng.ui.bootstrap.IModalService,
private projectsSvc: IProjectsService,
private attributesSvc: IAttributesService,
private swangular: any) { }
...
}
由于我没有使用@types for swangular,我只使用any
作为类型。
接下来,我尝试这样称呼它:
this.swangular.swal('Swangular Demo', 'Great demo, right?', 'question');
我收到以下错误:
TypeError:this.swangular.swal不是函数
我已经包含了以下必要的js文件:
<script type="text/javascript" src="../node_modules/sweetalert2/dist/sweetalert2.js"></script>
<script type="text/javascript" src="../node_modules/swangular/swangular.js"></script>
我也没有区别并在js文件的开头导入依赖项,如import * as angular from 'angular';
。也许我用错了方法?
我感谢任何帮助。
答案 0 :(得分:1)
$inject
注释和构造函数参数不匹配。
static readonly $inject = ["$scope", "$http", "$uibModal", "swangular", ProjectsService.serviceName, AttributesService.serviceName];
constructor(
private $scope: ng.IScope,
private $http: ng.IHttpService,
private $uibModal: ng.ui.bootstrap.IModalService,
private projectsSvc: IProjectsService,
private attributesSvc: IAttributesService,
private swangular: any) { }
它们应该按照相同的顺序排列,而且它们不是。 Angular无法确定第4个$inject
元素("swangular"
)是否匹配第6个构造函数参数(private swangular: any
)。