2016-09-01 44 views
0

我已經使用angular-cli設置了Angular 2(RC4)並使用了[email protected]爲TypeScript 1.8安裝jQuery 2.2.4類型

npm install [email protected] --save

增加"jquery": "^2.2.4"安裝的jQuery中的package.json

和分型與

typings install dt~jquery --global --save

增加"jquery": "registry:dt/jquery#1.10.0+20160704162008"在typings.json(注:此類型是1.x和2.x版本!)

現在當我使用我的任何打字稿文件時,TSC回報import $ from 'jquery'import * as $ from 'jquery'

[ts]無法找到模塊'jquery'。

有人可以告訴我爲什麼嗎?我究竟做錯了什麼?

+0

這不涉及到你的問題,但你確定你需要的jQuery與Angular2?它在一起工作不好。 –

+0

是的,我需要一個依賴於jQuery的外部依賴。這很糟糕,但在NG2大會上,目前沒有太多的項目可以使用。但多虧了在這裏提到。 –

回答

0

我解決它沒有火箭科學,只是做舊的方式。看起來RC4在正確解析類型時遇到了一些麻煩。但由於我是一名軟件開發人員,所有角度相關的「RC東西」在上次都發生了很大的變化,所以很難爲任何RC解決這個問題,所以我決定像這樣做一些老派:

declare var $: any;     // <-- yep, that's it 
declare var FlipClock: any;   // <-- same here 

@Component({ 
    selector: 'my-component', 
    moduleId: module.id, 
    templateUrl: './template.html', 
    styleUrls: ['./styles.css'] 
}) 

export class MyComponent implements AfterViewInit { 
    @ViewChild('flipclock') flipClock: ElementRef; 
    errors: any; 

    constructor() { 
    } 

    ngAfterViewInit() { 
    $(this.flipClock.nativeElement).FlipClock({ // <-- we must make sure that 
     clockFace: 'TwentyFourHourClock'   //  at runtime $ and FlipClock 
               //  exists 
    }); 
    } 
} 

提供jQuery和FlipClock在運行時,我們只需要追加在我們index.html腳本:

<!doctype html> 
<html> 
    <head> 
    <meta charset="utf-8"> 
    <title>My ng2-rc4 app</title> 
    <base href="/"> 

    <meta name="viewport" content="width=device-width, initial-scale=1"> 

    <link rel="stylesheet" href="vendor/flipclock/compiled/flipclock.css"> 
    <link rel="stylesheet" href="app/main.css"> 
    </head> 

    <body> 
    <my-app>Loading ...</my-app> 

    <script src="/vendor/jquery/dist/jquery.min.js"></script> 
    <script src="/vendor/flipclock/compiled/flipclock.min.js"></script> 
    </body> 
</html>