I cloned the following: https://github.com/AngularClass/angular2-webpack-starter
I want to use jquery and bootstrap. I AM:
npm install jquery bootstrap
Then I go to vendor.ts add import.
import 'jquery'; import 'bootstrap/dist/js/bootstrap'; import 'bootstrap/dist/css/bootstrap.min.css';
Then I went to webpack.common.js and added to the "plugins: [...]":
new webpack.ProvidePlugin({ jQuery: 'jquery', $: 'jquery', jquery: 'jquery' })
To test this, I changed home.component.html and added id = "testdiv" to one of the divs.
In home.component.ts I add "$ ('# testdiv'). Html (" JQuery Works ").
When I start npm, I get a bunch of errors:
error_handler.js:46 EXCEPTION: Uncaught (in promise): ReferenceError: $ is not definedErrorHandler.handleError @ error_handler.js:46next @ application_ref.js:298schedulerFn @ async.js:89SafeSubscriber.__tryOrUnsub @ Subscriber.js:223SafeSubscriber.next @ Subscriber.js:172Subscriber._next @ Subscriber.js:125Subscriber.next @ Subscriber.js:89Subject.next @ Subject.js:55EventEmitter.emit @ async.js:81onError @ ng_zone.js:123onHandleError @ ng_zone_impl.js:62ZoneDelegate.handleError @ zone.js:336Zone.runGuarded @ zone.js:242_loop_1 @ zone.js:508drainMicroTaskQueue @ zone.js:515ZoneTask.invoke @ zone.js:437 error_handler.js:51 ORIGINAL STACKTRACE:ErrorHandler.handleError @ error_handler.js:51next @ application_ref.js:298schedulerFn @ async.js:89SafeSubscriber.__tryOrUnsub @ Subscriber.js:223SafeSubscriber.next @ Subscriber.js:172Subscriber._next @ Subscriber.js:125Subscriber.next @ Subscriber.js:89Subject.next @ Subject.js:55EventEmitter.emit @ async.js:81onError @ ng_zone.js:123onHandleError @ ng_zone_impl.js:62ZoneDelegate.handleError @ zone.js:336Zone.runGuarded @ zone.js:242_loop_1 @ zone.js:508drainMicroTaskQueue @ zone.js:515ZoneTask.invoke @ zone.js:437 error_handler.js:52 Error: Uncaught (in promise): ReferenceError: $ is not defined
How do I get around this? I also tried changing webpack.common.js as follows:
new webpack.ProvidePlugin({ 'window.jQuery': 'jquery', 'window.$': 'jquery', })
Also did not work ...
If I go to the index.html page ... if I add:
<script src="http://code.jquery.com/jquery-3.1.1.min.js"></script>
This works, but I want to avoid this ...