Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to resolve "ReferenceError: $ is not defined"?

Tags:

angular

I am trying to use jquery in angular2 and got this error: ReferenceError: $ is not defined from the following code.

declare var $:JQueryStatic;

export class AppComponent {    

ngOnInit() {    

    var container = $("#contact-us-form");    

    container.css("width", $(document).width()).css("height", $(document).height()).hide();    

    container.find(".modal-content .button-close").click(function(){ this.toggleModalWindow("contact-us-form"); });    

    $("#contact-link").click(function(){ this.toggleModalWindow("contact-us-form"); });    

   }     
}      

How could I resolve this ?

like image 808
raj m Avatar asked Mar 11 '23 13:03

raj m


2 Answers

add angular-cli.json

 "../node_modules/jquery/dist/jquery.js"

Have a typescript file

  import * as $ from "jquery";

install jquery using npm

like image 155
raj m Avatar answered Mar 19 '23 14:03

raj m


FOLLOW THESE STEPS:

  1. npm i jquery --save
  2. npm i @types/jquery -D
  3. import * as $ from 'jquery'---> in app.module.ts

That should do the trick and no need to put declare var jQuery: any; or declare var $: any; in each file.

like image 28
gildniy Avatar answered Mar 19 '23 16:03

gildniy