Angular 2+ Import a Non Module into TS

天涯浪子 提交于 2021-01-29 14:29:09

问题


I made a library and it needs to support multiple applications. However one of the sites is non-angular and will not support "modules", hence I am unable to use "export" on my function.

My External library:

var foo = (function() {

    function MyFunction(){
    }

    MyFunciton.prototype.bar(){
    }

    return MyFunction;

}());

My Question: How do I import this into my component.ts without an export?


回答1:


Include your script in your angular.json file in the scripts section.

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

In your component service, declare your variable. You can then use it without typescript complaining.

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

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


来源:https://stackoverflow.com/questions/60758943/angular-2-import-a-non-module-into-ts

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!