Angular2 - Problem running jquery, $ is not defined #1232

Closed
opened 2025-08-09 17:19:21 +00:00 by fergalmoran · 0 comments
Owner

Originally created by @AndrewKralovec on 11/26/2016

I'm trying to use jquery in my app. Previoulsy in rc-4 i would just declare var : any; and then call it on after OnInit, but when i try to do this in my rc-5 build, it throws is not defined.

Component

import { Component, OnInit } from '@angular/core';
declare var $: any;
 
 @Component({
    selector: 'nav-menu',
    templateUrl: './navmenu.component.html',
    styleUrls: ['./navmenu.component.css']
})
export class NavMenuComponent implements OnInit {
    ngOnInit() {
        // Initialize collapse button
        $(".button-collapse").sideNav();
    }
}

Error


EXCEPTION: $ is not defined  vendor.js:5364:14
ORIGINAL STACKTRACE:  vendor.js:5369:18
[30]/NavMenuComponent</NavMenuComponent.prototype.ngOnInit@http://localhost:5000/dist/0.9ef51c72ff32ccd5ee38.hot-update.js:71:10
anonymous/_View_AppComponent0.prototype.detectChangesInternal@AppComponent.ngfactory.js:86:58
vendor_e1c95acbfa6e50db68d4</</</AppView</AppView.prototype.detectChanges@http://localhost:5000/dist/vendor.js?v=FG8DMO6mAWOeL29rrZABZrd79JSnjdqetNbUsSmBST0:11464:14
*Originally created by @AndrewKralovec on 11/26/2016* I'm trying to use jquery in my app. Previoulsy in rc-4 i would just declare var $: any; and then call it on after OnInit, but when i try to do this in my rc-5 build, it throws $ is not defined. Component ``` import { Component, OnInit } from '@angular/core'; declare var $: any; @Component({ selector: 'nav-menu', templateUrl: './navmenu.component.html', styleUrls: ['./navmenu.component.css'] }) export class NavMenuComponent implements OnInit { ngOnInit() { // Initialize collapse button $(".button-collapse").sideNav(); } } ``` Error ``` EXCEPTION: $ is not defined vendor.js:5364:14 ORIGINAL STACKTRACE: vendor.js:5369:18 [30]/NavMenuComponent</NavMenuComponent.prototype.ngOnInit@http://localhost:5000/dist/0.9ef51c72ff32ccd5ee38.hot-update.js:71:10 anonymous/_View_AppComponent0.prototype.detectChangesInternal@AppComponent.ngfactory.js:86:58 vendor_e1c95acbfa6e50db68d4</</</AppView</AppView.prototype.detectChanges@http://localhost:5000/dist/vendor.js?v=FG8DMO6mAWOeL29rrZABZrd79JSnjdqetNbUsSmBST0:11464:14 ```
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github/JavaScriptServices#1232
No description provided.