简体   繁体   English

在Angular中导入和使用外部JS库

[英]Import and use external JS Library in Angular

I have an external library (web-designer.js by Grapecity) which runs fine on plain HTML application.我有一个外部库(Grapecity 的 web-designer.js),它在普通的 HTML 应用程序上运行良好。 I want to use this library as it is in one of my Angular Components.我想使用这个库,因为它在我的 Angular 组件之一中。 I have added reference to this library, but I get errors while importing the file in Angular.我添加了对这个库的引用,但是在 Angular 中导入文件时出现错误。

This is how I reference the library (jQuery is a dependency):这就是我引用库的方式(jQuery 是一个依赖项):

    import '../app/lib/scripts/jquery';
    import '../app/lib/scripts/web-designer';

The cdn link for library is http://cdn.grapecity.com/ActiveReports/AR13/webdesignerresourcefiles/web-designer.js库的 CDN 链接是http://cdn.grapecity.com/ActiveReports/AR13/webdesignerresourcefiles/web-designer.js

These are the errors that I get while loading Angular application.这些是我在加载 Angular 应用程序时遇到的错误。 Please note that I get same errors when I use require instead of import .请注意,当我使用require而不是import时,我会遇到同样的错误。 The dependencies 'Barcode' and 'dv' are within web-designer library but Angular is unable to find it.依赖项 'Barcode' 和 'dv' 在 web-designer 库中,但 Angular 无法找到它。

在此处输入图像描述

There seems some issue in exporting the modules in the JS library.在 JS 库中导出模块似乎存在一些问题。 I am able to refer jQuery in the same way but not web-designer.js.我可以以同样的方式引用 jQuery 但不是 web-designer.js。

I want to modify the JS library so that I am able to consume it directly (as it works fine on plain HTML application).我想修改 JS 库以便能够直接使用它(因为它在普通的 HTML 应用程序上运行良好)。 The file (given in link above) is large and minified and I am unable to modify it to suit my needs.该文件(在上面的链接中给出)又大又小,我无法修改它以满足我的需要。

Looking for solutions if anyone has faced similar issue.如果有人遇到类似问题,请寻找解决方案。

Why dont you just import external lib in angular.json like this?为什么不像这样在 angular.json 中导入外部库?

  "architect": {
    "build": {
      "builder": "@angular-devkit/build-angular:browser",
      "options": {
        "outputPath": "dist/testssss",
        "index": "src/index.html",
        "main": "src/main.ts",
        "polyfills": "src/polyfills.ts",
        "tsConfig": "tsconfig.app.json",
        "aot": false,
        "assets": [
          "src/favicon.ico",
          "src/assets"
        ],
        "styles": [
          "src/styles.scss"
        ],
        "scripts": [] // add to script section here
      },

I have faced a similar issue and resolved it in this way.我遇到了类似的问题并以这种方式解决了它。 Have a look it may help you.看看它可能对你有帮助。 I used DragonSpeech detection cdn in this way in the.ts file and works fine.我在.ts文件中以这种方式使用了DragonSpeech检测cdn并且工作正常。

initiateDragonSpeechToText() {
    const fileref = document.createElement('script');
    fileref.setAttribute('type', 'text/javascript');
    fileref.setAttribute('src',
        'https://speechanywhere.nuancehdp.com/3.0/scripts/Nuance.SpeechAnywhere.js?_r=' +
        (Math.random() * Math.pow(10, 18)).toString(32));
    document.getElementsByTagName('head')[0].appendChild(fileref);

    const inlineFunc = document.createElement('script');
    inlineFunc.setAttribute('type', 'text/javascript');
    const userID = this.UserDetails ? this.UserDetails.UserName : '';
    inlineFunc.appendChild(document.createTextNode('function NUSA_configure() {NUSA_userId = "' + userID + '";NUSA_applicationName = "Artisan";NUSA_enableAll = false;}'));
    document.getElementsByTagName('head')[0].appendChild(inlineFunc);
}

We are reaching out to our development team to see if they can provide a solution for you.我们正在联系我们的开发团队,看看他们能否为您提供解决方案。

Thanks,谢谢,

GrapeCity Support Team葡萄城支持团队

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM