Angular 2+将非模块导入TS [英] Angular 2+ Import a Non Module into TS

查看:53
本文介绍了Angular 2+将非模块导入TS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个库,它需要支持多个应用程序.但是,其中一个站点是非角度的,将不支持模块",因此我无法在函数上使用导出".

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.

我的外部库:

var foo = (function() {

    function MyFunction(){
    }

    MyFunciton.prototype.bar(){
    }

    return MyFunction;

}());

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

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

推荐答案

将脚本包含在 scripts 部分的 angular.json 文件中.

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
}

这篇关于Angular 2+将非模块导入TS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆