【问题标题】:Angular 2+ Import a Non Module into TSAngular 2+ 将非模块导入 TS
【发布时间】:2020-06-30 16:36:57
【问题描述】:

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

我的外部图书馆:

var foo = (function() {

    function MyFunction(){
    }

    MyFunciton.prototype.bar(){
    }

    return MyFunction;

}());

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

【问题讨论】:

    标签: angular typescript ecmascript-6 ecmascript-5


    【解决方案1】:

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

      "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
    }
    

    【讨论】:

      猜你喜欢
      • 2016-06-28
      • 1970-01-01
      • 2017-07-11
      • 1970-01-01
      • 2021-08-06
      • 1970-01-01
      • 2017-10-01
      • 1970-01-01
      • 2021-11-23
      相关资源
      最近更新 更多