2017-02-09 123 views
0

我在我的项目中添加了第三方包(jQuery,colorbox)。首先,我对NPM安装掷和后我添加到 “角cli.json”Angular 2中的第三方库

"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> 

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

+0

提供plunker –

回答

0

您需要使用一个加载器将jQuery lib传递给您的angular 2项目。同时你也需要确保jquery类型在你的transpile时间里可用。

你可以使用systemjs loader和在系统配置中添加jquery路径在你的配置和你的打字机将理解jQuery。

如果你使用systemjs loader,它也可以在浏览器中使用。

+0

我在哪里可以找到Angular2 CLI中的systemjs它应该在webpack中,因为我找不到webpack配置。 – norbertsari

+0

是的,它在beta 14之后被移除了。你可以自己实现它。它的分开。如果你正在使用angular-cli,那么它会自动启动 –