I want to include jQuery and SignalR in my Angular2 -Application and connect everything using webpack.
So I installed jQuery through npm.
package.json
"dependencies": { // ... "jquery": "2.1.4", // ... },
Files and folders are installed correctly.
Now I am targeting these files in my vendor.ts to get a web package to find and enable them:
import 'jquery'; import 'bootstrap/dist/js/bootstrap.js'; import '../../bower_components/signalr/jquery.signalR';
And webpack receives these files and prepares them. I see this in my vendor.js. There is jQuery here. Also jquery signalR-File.
My webpack.config:
var webpack = require("webpack"); var HtmlWebpackPlugin = require('html-webpack-plugin'); module.exports = { entry: { "vendor": "./wwwroot/app/vendor", "polyfills": "./wwwroot/app/polyfills", "app": "./wwwroot/app/boot" }, output: { path: __dirname, filename: "[name]-[hash:8].bundle.js" }, resolve: { extensions: ['', '.ts', '.js', '.html'] }, devtool: 'source-map', module: { loaders: [ { test: /\.ts/, loaders: ['ts-loader'], exclude: /node_modules/ }, { test: /\.html$/, loader: 'html' }, { test: /\.css$/, loader: 'raw' } ] }, plugins: [ new webpack.ProvidePlugin({ jQuery: 'jquery', $: 'jquery', jquery: 'jquery' }), //new webpack.optimize.DedupePlugin(), new webpack.optimize.CommonsChunkPlugin({ name: ["app", "vendor", "polyfills"] }) ] }
Loaded into my index, for example:
<script src="js/polyfills-2eba52f6.bundle.js"></script> <script src="js/vendor-2eba52f6.bundle.js"></script> <script src="js/app-2eba52f6.bundle.js"></script>
But when I try to use it in my angular2 component, for example:
// ... declare var jQuery:any; declare var $:any; @Injectable() export class MySignalRService { private connection; constructor() { this.connection = $.hubConnection(CONFIGURATION.baseUrls.server + 'signalr/'); jQuery.hubConnection(CONFIGURATION.baseUrls.server + 'signalr/'); // ... } //...
I always get a message that
$. hubConnection is not a function
Error: jQuery not found. Please make sure jQuery is referencing the SignalR client JavaScript file before.
leak "$" and "jquery" undefined.
What can I do to access singalr function in webpack?
BR Tenoda