5

I'm trying to figure out how to use jQuery globally within my angular 2 app, the only reasonable source I found so far is this stackoverflow answer However I can't seem to get it working. Is there a way to achieve this with npm package management instead of tsd (this command is not working for me, I assumed it is coming from typescript, but seems like not).

After this, I think I need to reference jQuery within my app.ts file? (main file used by bootstrap.ts to boot the application)

Here is my project structure

app
  components
    app.ts
  services
  typings
  bootstrap.ts
index.html

and few code examples I believe need to be used for jQuery implementation

app.ts

import {Component, View} from 'angular2/core';

@Component({
    selector: 'app',
    templateUrl: 'app/components/app.html'
})

export class App { }

bootstrap.ts

import {App}              from './components/app';

bootstrap(App);
Oleksii Pavlenko
  • 1,327
  • 1
  • 11
  • 25
Ilja
  • 44,142
  • 92
  • 275
  • 498

1 Answers1

5

Install the jquery typings using:

npm install @types/jquery

Include the jQuery.js file in your index.html

And.. that's it I guess :)

Good luck

Poul Kruijt
  • 69,713
  • 12
  • 145
  • 149
  • And I assume I need to include desired version of jQuery in html right? – Ilja Jan 13 '16 at 09:53
  • Yes, indeed. A reference in typescript tells the compiler that you take care of adding the required javascript – Poul Kruijt Jan 13 '16 at 13:23
  • 1
    fyi, tsd is the typescript definition manager. You can install this by running `npm -g install tsd`. After that you can install definition files like jQuery by running `tsd install jquery` :). This way you do not have to download them manually – Poul Kruijt Jan 13 '16 at 14:34
  • @PierreDuc Now on `angular 2` how is the `typings` used as we don't use typings anymore, and we use `npm` to get the dependencies. – PaladiN Dec 22 '16 at 04:33
  • @PaladiN You can install them using this: `npm install @types/jquery`. Together with including the jquery.js in your index.html – Poul Kruijt Dec 22 '16 at 07:53