首页 > 解决方案 > Angular 2+将非模块导入TS

问题描述

我制作了一个库,它需要支持多个应用程序。然而,其中一个站点是非角的,不支持“模块”,因此我无法在我的函数上使用“导出”。

我的外部图书馆:

var foo = (function() {

    function MyFunction(){
    }

    MyFunciton.prototype.bar(){
    }

    return MyFunction;

}());

我的问题:如何在没有导出的情况下将其导入到我的 component.ts 中?

标签: angulartypescriptecmascript-6ecmascript-5

解决方案


在该部分的angular.json文件中包含您的脚本。scripts

  "architect": {
    "build": {
      "builder": "@angular-devkit/build-angular:browser",
      "options": {
       //...
        "scripts": [
          "src/assets/foo.js.js"
        ]
      },

在您的组件服务中,声明您的变量。然后,您可以在没有打字稿抱怨的情况下使用它。

//declare this on top of your component/service
declare let foo: any;

myMethod()
{
 var inst = new foo();
 inst.bar();//call bar
}

推荐阅读