简体   繁体   English

以正确的方式在TypeScript + AngularJS中导入/注入模块

[英]import/inject modules in TypeScript+AngularJS the right way

I've downloaded this module https://github.com/skeymeulen/swangular via npm and want to use it with TypeScript. 我已经通过npm下载了这个模块https://github.com/skeymeulen/swangular ,并希望将它与TypeScript一起使用。 Now I am struggling including the dependencies the correct way. 现在我正在努力包括依赖关系的正确方法。 Like the READ.me on GitHub tells me I am injecting the module in my app like this: 就像GitHub上的READ.me告诉我,我正在我的应用程序中注入模块,如下所示:

var app = angular.module("app", ['ui.bootstrap', 'swangular']);

My Controller looks like this then: 我的控制器看起来像这样:

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) { }

...
}

Since I got no @types for swangular, I just use any as type. 由于我没有使用@types for swangular,我只使用any类型。

Next, I try to call it like this: 接下来,我尝试这样称呼它:

this.swangular.swal('Swangular Demo', 'Great demo, right?', 'question');

I receive the following error: 我收到以下错误:

TypeError: this.swangular.swal is not a function TypeError:this.swangular.swal不是函数

I've included the following necessary js files: 我已经包含了以下必要的js文件:

<script type="text/javascript" src="../node_modules/sweetalert2/dist/sweetalert2.js"></script>
<script type="text/javascript" src="../node_modules/swangular/swangular.js"></script>

I also don't get the difference between that and importing a dependency in the beginning of the js file like import * as angular from 'angular'; 我也没有区别并在js文件的开头导入一个依赖项,如import * as angular from 'angular'; . Maybe I'm using the wrong way? 也许我用错了方法?

I appreciate any help. 我感谢任何帮助。

$inject annotation and constructor parameters don't match. $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) { }

They should come in the same order, and they don't. 它们应该按照相同的顺序排列,但它们不是。 There's no way how Angular can figure out that 4th $inject element ( "swangular" ) matches 6th constructor param ( private swangular: any ). Angular无法确定第4个$inject元素( "swangular" )是否匹配第6个构造函数param( private swangular: any )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM