2016-11-09 8 views
2

すべて 私はAngular 2を使用して小さなWebアプリケーションを作成しています。 私はここで が誤りである理由を私は知らないクロームでエラーを取得しています:

core.umd.js:3004 EXCEPTION: Uncaught (in promise): Error: Error in `enter code here`http://localhost:3000/app/dashboard.component.js class DashboardComponent_Host - inline template:0:0 caused by: $ is not defined 
ReferenceError: $ is not defined 
    at DashboardComponent.ngOnInit (http://localhost:3000/app/dashboard.component.js:22:9) 
    at Wrapper_DashboardComponent.detectChangesInInputProps (/AppModule/DashboardComponent/wrapper.ngfactory.js:18:53) 
    at _View_DashboardComponent_Host0.detectChangesInternal (/AppModule/DashboardComponent/host.ngfactory.js:30:32) 
    at _View_DashboardComponent_Host0.AppView.detectChanges (http://localhost:3000/node_modules/@angular/core/bundles/core.umd.js:9305:18) 
    at _View_DashboardComponent_Host0.DebugAppView.detectChanges (http://localhost:3000/node_modules/@angular/core/bundles/core.umd.js:9410:48) 
    at ViewRef_.detectChanges (http://localhost:3000/node_modules/@angular/core/bundles/core.umd.js:7398:24) 
    at RouterOutlet.activate (http://localhost:3000/node_modules/@angular/router/bundles/router.umd.js:3458:46) 
    at ActivateRoutes.placeComponentIntoOutlet (http://localhost:3000/node_modules/@angular/router/bundles/router.umd.js:2955:20) 
    at ActivateRoutes.activateRoutes (http://localhost:3000/node_modules/@angular/router/bundles/router.umd.js:2933:26) 
    at eval (http://localhost:3000/node_modules/@angular/router/bundles/router.umd.js:2902:23) 

そしてここでは私のコードです:

import { Component, ElementRef, OnInit } from '@angular/core'; 
declare var $:JQueryStatic; 
@Component({ 
    moduleId: module.id, 
    selector: 'my-dashboard', 
    templateUrl: 'dashboard.component.html', 
    styleUrls: ['dashboard.component.css'], 
}) 
export class DashboardComponent implements OnInit{ 

    constructor(private elRef: ElementRef){} 

    ngOnInit():any{ 
     $(this.elRef.nativeElement).find('button').on('click', function() { 
      alert('It works!'); 
     }); 
    } 
} 

私はjqueryのをインストールするには、この答えを使用していました説明されているものすべてが動作しません。 link なぜでしょうか? おかげ


FIX は私がdeclare var $:any;declare var $:JQueryStatic;を変更することで、それを固定してjquery.jsが実際に例えばChromeデベロッパーツールを使用してロードされているかどうかを確認してくださいindex.html

+0

にCDNを使用しますが、 'jqueryの' からの 'の輸入$のようなものを試してみましたか;'? –

+0

@DanielD私は今、モジュールのjqueryにデフォルトのエクスポートがないと言っています。 –

+0

この投稿をチェックしてください:http://stackoverflow.com/questions/35223977/jspm-jquery-typescript-module-jquery- has-no-default-export –

答えて

-1

<script src="https://code.jquery.com/jquery-2.2.0.min.js"></script>が含まれています。そうでなければ、モジュールローダーの設定に依存していません。

jquery.js依存関係が、選択したモジュールローダに認識されていることを確認する必要があります。

+0

「jquery」として宣言された 'package.jason'にあります:"^3.1.1 "'何らかの方法でインストールすればいいですか? –

+0

@JakubBadaczどこから始めましたか?あなたのコードを共有できますか? ) – wzr1337

+0

それは大丈夫、私はそれを考え出した:) –

1

layout.cshtml

<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.js" ></script> 
関連する問題