簡體   English   中英

以正確的方式在TypeScript + AngularJS中導入/注入模塊

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

我已經通過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'; 也許我用錯了方法?

我感謝任何幫助。

$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個構造函數param( private swangular: any )。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM