2017-01-22 6 views
2

私はAngular2アプリケーションをスカフォールドするためにangular-cliを使用しています。Angular2コンポーネントでSignalRをインポートするには?

以下

package.jsonからの抜粋です:

"dependencies": { 
    // ... 
    "jquery": "^3.1.1", 
    "signalr": "^2.2.1" 
    //... 
}, 
"devDependencies": { 
    // ... 
    "@types/jquery": "^2.0.39", 
    "@types/signalr": "^2.2.33" 
    //... 
} 

マイapp.component.ts

import { Component } from '@angular/core'; 
import * as $ from "jquery"; 
import "signalr"; 

@Component({ 
    selector: 'app-root', 
    templateUrl: './app.component.html', 
    styleUrls: ['./app.component.css'] 
}) 
export class AppComponent { 
    title = 'app works!'; 
    constructor() { 
     console.log($); 
    } 
} 

は今、私はjQueryのが追加されていないと言ってSignalRから以下のエラーが出ますが、私はそれをインポートしました上の行:

Error: jQuery was not found. Please ensure jQuery is referenced before the SignalR client JavaScript file.

私はimport "signalr"; jQueryが正しくロードされます。ここで何が起きてるの?なぜSignalRはjQueryが見つからないと言っていますか?

答えて

1

私はこの問題をこのように解決:

import $ from 'jquery'; 
window.jQuery = $; 
require('signalr'); 

Signalrはwindow.jQueryに依存しています。何らかの理由でimport $ from 'jquery'window.jQueryに設定されていません。それが明示的に行う必要がある理由です。

関連する問題