2017-06-16 9 views
2

私のangluar cliプロジェクトでjQueryを使いたいです。 私はここの手順を試してみました:https://www.gurustop.net/blog/2016/11/18/3821/jquery-angular2-angularcli-how-to角度のあるcliプロジェクトにjQueryを追加できません

  1. はjQueryの

    型チェックのためのjQueryのタイプdefinationをインストール
    npm install --save jquery; 
    
  2. をインストールします。

    npm install --save-dev @types/jquery 
    
  3. アンギュラcli.jsonファイル内の "スクリプト" 配列にjqueryのファイルのrefeneceを追加します。

    ERROR in ./src/app/app.component.ts (2,25): Module ''jquery'' resolves 
    to a non-module entity and cannot be imported using this construct. 
    

    私がもし:私は次のエラーを取得するng buildを行うときに、私のapp.component.tsで

    "scripts": [ 
        "../node_modules/jquery/dist/jquery.min.js" 
    ] 
    
  4. インポートjqueryのは、

    import { Component } from '@angular/core'; 
    import * as jQuery from 'jquery'; 
    
    @Component({ 
        selector: 'app-root', 
        templateUrl: './app.component.html', 
        styleUrls: ['./app.component.scss'] 
    }) 
    export class AppComponent { 
        title = 'app'; 
        test = jQuery("body"); 
    } 
    
  5. しかし

ファイルimport * as jQuery from 'jquery';を削除し、 `ng build 'を削除します。

ERROR in ./src/app/app.component.ts (10,10): Cannot find name 'jQuery'. 

ここからどこへ行くかわかりません。 @Saravanaが掲示何に基づいて

アップデート1

。 私はちょうどdeclare const $: JQueryStaticng build行うと、私はエラーを取得:

ERROR in ./src/app/app.component.ts (2,19): Cannot find name 'JQueryStatic'. 

を、私は彼がそれを削除する前にSaravanaは彼の最初のポストに提案何場合。

import "jquery"; 
declare const $: JQueryStatic; 

これは文句なしで作成されます。だから私はサラバナあなたのポストを元に戻す必要がありますと思う。

答えて

2

jQueryライブラリはグローバル変数$jQueryを追加しますので、あなただけの変数を宣言することができます

最新のCLIへのアップグレード
import "jquery"; 
declare const $: JQueryStatic; 
0

は、新しいプロジェクトを作成し、私のビットで、指示に移動しました上で概説したものはうまく機能しました。

npm uninstall -g angular-cli 

npm cache clean 

npm install -g @angular/[email protected] 
関連する問題