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

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

问题描述

我使用 Ionic 2、Angular 2 和 TypeScript 构建了一个 Ionic 项目来稍微测试一下框架.我需要在我的项目中包含一个外部库(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.

我知道在 TypeScript 中包含一个 Javascript 库应该可以工作,因为在 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?

推荐答案

您可以像添加任何其他常规 JavaScript 文件一样将其添加到 index.html 中.

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 文件中.这行代码基本上是在告诉打字稿编译器,那里有一个应该忽略的变量 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天全站免登陆