2017-02-09 127 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,那麼它會自動啓動 –