sameera207 sameera207 - 1 year ago 126
TypeScript Question

adding a js library (not yet in typings) to an ionic2 project

I'm trying to use this external javascript library CamanJS with my Ionic2 (typescript project)

I've found several articles like this and this, however all of them assumes the library that is going to use is in typings

But CamanJS is not yet in the typings and there is a open ticket for it

my question is, is there a way to add an external js library (which is not yet added to typings) to an ionic2 (typescript) project?

Answer Source

Let me describe this process in a common case:

  1. To include a JavaScript library in your TypeScript application you must first include it in your HTML file as a script tag.

    <script src="js/your_external_library.js"></script>

  2. To use the library you must add the following to one of your ts files:

    declare var libraryVar: any;

  3. Replace libraryVar with a variable, function, or class within your JavaScript library.

At this point it is ready for use.

  1. After these steps you can use your external library with API which it provides. For example:

    this.libraryVar = new libraryVar(parameters);

  2. Call tsc and .ts file compiled into .js with properly syntax and with your external library.