2

I am trying to set up angular 2 app based on webpack. I see that in vendor.ts we have that structure.

// Angular 2
import '@angular/platform-browser';
import '@angular/platform-browser-dynamic';
import '@angular/core';
import '@angular/common';
import '@angular/http';
import '@angular/router';

// RxJS
import 'rxjs';


// Other vendors for example jQuery, Lodash or Bootstrap
// You can import js, ts, css, sass, ...

I want to import jQuery (for example) but I don't know what. Because when I tried to do that but it's not working. What I'm doing wrong? Thanks for any help.

import 'jquery/dist/jquery.min.js';

So I don't have idea why rxjs imported OK but jquery not :)

5
  • Try import $ from 'jquery' Commented Oct 31, 2016 at 10:10
  • Hi Alexey. It is working, but I don't uderstand why they import import '@angular/core'; in vendor.ts and then they do the same in inner modules/components. Should I do the same with jQuery? import jQuery to the vendor.ts and after that do the same in inner modules? Commented Oct 31, 2016 at 10:22
  • I may be wrong (just starting with ng2 myself) but I think that by importing angular/core in vendor.ts you tell webpack to include that module into the resulting bundle, and later in your code you usually import some specific parts of the module like import { NgModule } from '@angular/core'; - that way you can use NgModule in your code. Commented Oct 31, 2016 at 10:29
  • Now I understand that moment. Thank you so much! Commented Nov 1, 2016 at 8:33
  • Glad I helped you, so I changed my comment to answer, you can accept it. Commented Nov 1, 2016 at 8:55

1 Answer 1

2

This is how I implemented it:

  1. I have an alias in resolve section of my Webpack config file:

    resolve: { ... alias: { jquery: "jquery/src/jquery" } }

    this will allow you to write

    import 'jquery';

    instead of

    import 'jquery/dist/jquery.min.js';

    in vendor.ts

  2. Use Webpack ProvidePlugin to make jQuery and $ global:

    plugins: [ ... new webpack.ProvidePlugin({ $: 'jquery', jQuery: 'jquery', "window.jQuery": 'jquery' }), ... ]

I may be wrong (just starting with ng2 myself) but I think that by importing angular/core in vendor.ts you tell Webpack to include that module into the resulting bundle, and later in your code you usually import some specific parts of the module like import { NgModule } from '@angular/core'; - that way you can use NgModule in your code.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.