简体   繁体   中英

How to import an external script in my Angular library?

I am trying to export some business logic ts files of my Angular application into an Angular library. Unfortunately, I need to use a Javascript file to connect to a proprietary service build by other people, which is only available as a pure javascript file and requires jQuery. Without libraries (when I was using the script in my angular application), I solved this by adding these js files to the.angular.json under the "scripts" section.

The js file is huge so I could not consider the option to rewrite in typescript.

Is there some way to import and use a javascript file in my ng library?

Thanks Karan

I have finally solved my issue. Turns out there is no way to add a javascript file to an Angular library since ng library does not support "script" tag.

There is one other way: ie you can publish your library without the javascript file dependency, and the client using it will have to do 2 things:

  1. npm install "your-library"
  2. Now add the dependent javascript file to scripts tag in angular.json file.

Example in my case:

Run npm install wps-ng

And my Angular.json looks like this:

"scripts": [ "node_modules/jquery/dist/jquery.js", "node_modules/wps-js-52-north/src/web/lib/wps-js-all.js" ]

Unlike the other libraries, just a simple npm install is not sufficient, the client will just need to add their javascript file to scripts tag.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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