Angular 2中的第三方库

时间:2017-02-09 21:47:25

标签: javascript angular

我在项目中添加了第三方包(jquery,colorbox)。首先我在npm上安装了throw,之后我添加了" angular-cli.json"

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

我在组件中声明了

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

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

和视图

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

当我第一次点击链接时,它会将我带到google firebase存储链接页面,之后当我回到网页并再次单击链接时,colorbox包工作正常。我调用了ngAfterViewInit(),但看起来并不存在。 为什么不第一次工作?

1 个答案:

答案 0 :(得分:0)

您需要使用一个加载器将jQuery lib传递给angular 2项目。并且您还需要确保在您的转换时间内可以使用jquery类型。

你可以使用systemjs loader,在系统配置中添加jquery路径,你的打字符会理解jquery。

如果您将使用systemjs加载程序,它也将在浏览器中可用。

相关问题