简体   繁体   English

如何在Angular 2 / Typescript应用程序中使用第三方js库?

[英]How to use a third party js library in Angular 2 / Typescript application?

I am trying to use a third party .js library in Angular 2 / Typescript application. 我试图在Angular 2 / Typescript应用程序中使用第三方.js库。 There are some examples on the web (like using declare var libraryVar: any;) but nothing seems to work. 网上有一些例子(比如使用declare var libraryVar:any;)但似乎没什么用。 Is there a one correct way to integrate a .js library into a Typescript / Angular2 ? 是否有一种正确的方法将.js库集成到Typescript / Angular2中?

There are some examples on the web (like using declare var libraryVar: any;) but nothing seems to work. 网上有一些例子(比如使用declare var libraryVar:any;)但似乎没什么用。 Is there a one correct way to integrate a .js library into a Typescript / Angular2 是否有一种正确的方法将.js库集成到Typescript / Angular2中

Perhaps you forgot to put the declare var libraryVar:any; 也许你忘了把declare var libraryVar:any; in a vendor.d.ts file that has no root level import/exports . vendor.d.ts文件中没有根级别导入/导出

More : https://basarat.gitbooks.io/typescript/content/docs/types/migrating.html 更多: https//basarat.gitbooks.io/typescript/content/docs/types/migrating.html

The web is full of examples of typescript projects consuming 3rd party libraries. Web上充满了使用第三方库的打字稿项目的示例。

Here is one of mine which uses arcgis-js-api. 是我的一个使用arcgis-js-api。

And Here is one that uses openlayers. 是一个使用openlayers的人。

In your case you would try this: 在你的情况下你会尝试这个:

bower init
bower install node-uuid --save
tsd install uuid --save

Then setup your tsconfig.json file. 然后设置您的tsconfig.json文件。 I think Angular 2 uses systemjs so you'd want to set module="system" (see system ) unless you have a reason not to. 我认为Angular 2使用systemjs所以你想要设置module =“system”(参见系统 ),除非你有理由不这样做。

I haven't had much luck with systemjs but you might need to do something like this to get it to load uuid or any other AMD modules: 我对systemjs没有太多运气,但你可能需要这样做才能让它加载uuid或任何其他AMD模块:

window.define = System.amdDefine;
window.require = System.amdRequire;

See system-api for an explanation. 有关说明,请参阅system-api

The best way I have found to use an external library is to create a variable for it at the top of your typescript and import the js. 我发现使用外部库的最好方法是在打字稿的顶部为它创建一个变量并导入js。 By doing so the variable will offer all the functions the library provides inside your TS without any extra work. 通过这样做,变量将提供库中提供的所有函数,而无需任何额外的工作。

for example if you want to use moment.js to do some time computation. 例如,如果你想使用moment.js做一些时间计算。 add this to your ts file 将其添加到您的ts文件中

var moment = require("./scripts/moment");

after that you can use the functions inside your typescript by directly using the variable 之后,您可以直接使用变量来使用打字稿中的函数

moment.fromNow("SOMEDATE");

I faced the exact problem couple of days ago and I saw this is a good fit, Another neat way is to create a typescript class with all the helpers and reference the typescript to your file and call the helpers within. 几天前我遇到了确切的问题,我看到这是一个很好的选择,另一个简洁的方法是创建一个包含所有帮助程序的typescript类,并将typescript引用到您的文件中并调用其中的帮助程序。 Still experimenting on a solid pattern, but this would be the start point. 仍在尝试实体模式,但这将是起点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM