简体   繁体   English

Angular 2中的第三方库

[英]Third party library in Angular 2

I added a third-party package (jquery, colorbox) to my project. 我在项目中添加了第三方程序包(jquery,colorbox)。 First I installed throw on npm and after I added to the "angular-cli.json" 首先,我在npm上安装throw,然后将其添加到“ angular-cli.json”中

"scripts": [
    "../node_modules/jquery/dist/jquery.js",
    "../node_modules/jquery-colorbox/jquery.colorbox.js", ....

and I declare in the component, 我在组件中声明

declare var $:any;
declare var colorbox:any;
and the code

ngAfterViewInit(){
    $(document).ready(function(){
      $(".alma1").colorbox({rel:'alma1'});
    });
}

and the view 和视图

<a class="alma1" href="{‌{pic.pictureURL}}" title="{‌{pic.desc}}">
    <img class="alma" src="{‌{pic.pictureURL}}" alt="{‌{pic.desc}}">    
</a>

When I click first time on the link it takes me to the google firebase storage link page and after when I go back to the web page and click again on the link then the colorbox package working fine. 当我第一次单击该链接时,它将带我到google firebase存储链接页面,而当我返回该网页并再次单击该链接之后,则colorbox包运行正常。 I called in the ngAfterViewInit(), but is look likes not exist. 我在ngAfterViewInit()中调用了,但是看起来好像不存在。 Why is not working at the first time? 为什么第一次不工作?

You need to use one loader to pass your jQuery lib to you angular 2 project. 您需要使用一个加载程序将jQuery库传递给angular 2项目。 and also you need to make sure that jquery types are available in your transpile time. 并且还需要确保jquery类型在您的转换时间内可用。

you can use systemjs loader and in system config add jquery path in you config and your typescript will understand jquery. 您可以使用systemjs loader并在系统配置中在配置中添加jquery路径,您的打字稿将理解jquery。

if you will use systemjs loader, it will also available in the browser. 如果您将使用systemjs loader,则它也将在浏览器中可用。

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

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