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

孤者浪人 提交于 2020-01-05 03:26:08

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/38578741/adding-a-js-library-not-yet-in-typings-to-an-ionic2-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!