I want to include jQuery and SignalR in my Angular2-Application and wire everything up with webpack.
I therefore installed jQuery via npm.
package.json
"dependencies": {
// ...
"jquery": "2.1.4",
// ...
},
Files and folders are installed correctly.
I now target these files in my vendor.ts to get webpack to find and include them:
import 'jquery';
import 'bootstrap/dist/js/bootstrap.js';
import '../../bower_components/signalr/jquery.signalR';
And webpack gets these files and wires them up. I can see this in my vendor.js. jQuery is present there. Also the 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 in my index like:
<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 like:
// ...
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 alway get the message that
$.hubConnection is not a function
Error: jQuery was not found. Please ensure jQuery is referenced before the SignalR client JavaScript file.
consoling out "$" and "jquery" is undefined.
What can I do to access the singalr-Function in webpack?
BR Tenoda