Angular 2+ 将非模块导入 TS

Angular 2+ Import a Non Module into TS

我做了一个库,它需要支持多个应用程序。但是,其中一个站点是非 angular 并且不支持 "modules",因此我无法在我的函数中使用 "export"。

我的外部图书馆:

var foo = (function() {

    function MyFunction(){
    }

    MyFunciton.prototype.bar(){
    }

    return MyFunction;

}());

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

将您的脚本包含在 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
}