How to install Jquery in Angular?

2

What is the correct way to install jquery on an angle project 2 + ?

With bootstrap:

npm install --save jquery@latest

Only it is not available in node_modules so I get the path and put it in angular.json

    
asked by anonymous 08.06.2018 / 16:56

3 answers

1

jQuery non-angular CLI

Install the package through:

  • npm install jquery — save

later in your ./angular-cli.json.

  • “scripts”: [ “../node_modules/jquery/dist/jquery.min.js” ] ,

You can also do the same with the bootstrap as an example:

  • "scripts": ["../ node_modules / bootstrap / dist / js / bootstrap.js"]

Finally include in your component:

  • import * as $ from ‘jquery’;

Obs :. $ will serve as your global reference for Jquery

import { Component, OnInit  } from '@angular/core';
import * as $ from 'jquery';
@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
  title = 'Look jQuery Animation working in action!';

  public ngOnInit()
  {
$(document).ready(function(){
    $("button").click(function(){
        var div = $("div");  
        div.animate({left: '100px'}, "slow");
        div.animate({fontSize: '5em'}, "slow");
    });
});
  }
    
11.06.2018 / 13:49
2

Try it anyway:

npm install jquery --save     

11.06.2018 / 11:56
2

Project Root:

npm install jquery --save

angular-cli.json

  "scripts": [
    "../node_modules/jquery/dist/jquery.min.js",
  ]

Ts:

   import $ from "jquery";

stackblitz

    
11.06.2018 / 13:17