如何在Ionic 2 TypeScript项目中包含外部Javascript库? [英] How to include external Javascript library in an Ionic 2 TypeScript project?

查看:91
本文介绍了如何在Ionic 2 TypeScript项目中包含外部Javascript库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Ionic 2,Angular 2和TypeScript构建一个离子项目来测试框架。我需要在我的项目中包含一个外部库( ntc.js ),因为我需要它来命名十六种颜色。

I build an Ionic Project using Ionic 2, Angular 2 and TypeScript to test the framework a bit. I need to include an external library (ntc.js) to my project, since I need it to name hex colors.

我知道包含一个Javascript库到TypeScript应该可以工作,因为JS中的任何工作都适用于TS。我只是不想以错误的方式包含它。

I know including a Javascript library to TypeScript should work, since whatever works in JS works in TS. I just don't want to include it the wrong way.

我试图将库添加到www / build / js,但它似乎不起作用它似乎不是这样做的好方法。我试图寻找方法来做到这一点但却一无所获(可能是因为Angular 2和Ionic 2仍然很新鲜)。

I tried to add the library to www/build/js, but it doesn't seem to work and it doesn't seem like the good way to do this. I tried to search for ways to do this but found nothing (might be because Angular 2 and Ionic 2 is still fresh).

类似的事情:

import * as ntc from '../../js/ntc';

似乎不能正常工作,即使我的图书馆位于正确的位置。 TypeScript似乎没有正确读取我的文件,如果它完全读取它。

doesn't seem to work as well, even if my library is located at the right place. TypeScript doesn't seem to read my file properly, if it reads it at all.

这样做的好方法是什么?我应该将我的.js文件放在我的项目目录中?

What is the good way to do this? Where should I place my .js file in my project directory?

推荐答案

您可以通过将其添加到index.html来导入它任何其他常规的javascript文件。

You import it by adding it to your index.html like any other regular javascript file.

然后在你的ts文件中你做:

Then in your ts file you do:

declare var Tree:any; 

然后在您的代码中,您可以使用Tree变量,尽管它存在于Javascript文件中。这行代码基本上告诉了typescript编译器,那里有一个变量,它应该忽略Tree。

Then in your code, you can use the Tree variable, albeit it exists in the Javascript file. this line of code is basically telling the typescript compiler there is a variable out there Tree which it should ignore.

这篇关于如何在Ionic 2 TypeScript项目中包含外部Javascript库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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