将一个js库(尚未列入)添加到ionic2项目中 [英] adding a js library (not yet in typings) to an ionic2 project

查看:144
本文介绍了将一个js库(尚未列入)添加到ionic2项目中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用这个外部JavaScript库 CamanJS 和我的Ionic2(打字稿项目)

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

我发现了一些文章,如这个这个但是,所有这些都假设将要使用的库是 typings

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

但是CamanJS还没有出现在这个类型中,并且有一个开放的

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

我的问题是,有没有办法将一个外部js库(尚未添加到打字中)添加到ionic2(typescript)项目中?

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?

推荐答案

让我在一个常见的案例中描述这个过程:

Let me describe this process in a common case:


  1. 进入在你的TypeScript应用程序中包含一个JavaScript库,你必须首先将它作为脚本标记包含在HTML文件中。

  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>

要使用该库,您必须将以下内容添加到其中一个库中 ts files:

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

声明var libraryVar:any;

用JavaScript库中的变量,函数或类替换 libraryVar

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

此时可以使用。

At this point it is ready for use.


  1. 完成这些步骤后,您可以将外部库与其提供的API一起使用。例如:

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

this.libraryVar = new libraryVar(parameters);

调用 tsc .ts 编译成的文件。 js 具有正确的语法和外部库。

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

这篇关于将一个js库(尚未列入)添加到ionic2项目中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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