How to import jquery and mCustomScrollbar plugin into Angular2 component

kszdev picture kszdev · Apr 20, 2016 · Viewed 6.9k times · Source

I have a problem with import these modules to my angular2 component. I using angular2-webpack-starter from AngularClass.

I install dependecies with npm:

npm install jquery --save
npm install malihu-custom-scrollbar-plugin --save

and install typings:

typings install jquery --save --ambient
typings install mcustomscrollbar --save --ambient

And i want to use it in component like this:

jQuery('.selector').mCustomScrollbar();

What is the best solution to do that?

I tried to use this solution: Whats the best way to use jquery widgets inside angular 2?

But it's not work, i got error "jQuery is not defined".

Answer

nthaxis picture nthaxis · Dec 10, 2016

USING
Angular: 2.0.0
Typescript: 2.0.2
Angular-cli: 1.0.0-beta.16
webpack: 2.1.0-beta.22
node: 4.6
npm: 2.15.9

ANSWER
Add types to src/tsconfig.json

SOLUTION

  1. Get Required packages
    npm install jquery malihu-custom-scrollbar-plugin --save
    npm install @types/jquery @types/jquery-mousewheel @types/mcustomscrollbar --save-dev

  2. Add plugin css and scripts to angular-cli.json in your root folder

    //angular-cli.json
    "apps": [
        {
            ...,
            "styles": [
                ...,
                "../node_modules/malihu-custom-scrollbar-plugin/jquery.mCustomScrollbar.css"
            ],
            "scripts": [
                "../node_modules/jquery/dist/jquery.js",
                "../node_modules/malihu-custom-scrollbar-plugin/node_modules/jquery-mousewheel/jquery.mousewheel.js",
                "../node_modules/malihu-custom-scrollbar-plugin/jquery.mCustomScrollbar.concat.min.js"
            ],
            ...
        }
    ]
    
  3. Add types to src/tsconfig.json

    //tsconfig.json
    {
        "compilerOptions": {
            ...,
            "typeRoots": [
                "../node_modules/@types/"
            ],
            "types": [
                "jquery","jquery-mousewheel","mCustomScrollbar"
            ],
            ...
        }
    }
    
  4. Make Directive

    //scrollbar.directive.ts
    import {Directive, ElementRef, OnInit} from '@angular/core';
    declare var $:any;  //<-- let typescript compiler know your referencing a var that was already declared
    
    @Directive({
        selector: 'scrollbar',
        host: {'class':'mCustomScrollbar'},  //<-- Make sure you add the class
    })
    export class ScrollbarDirective implements OnInit {
        el: ElementRef;
        constructor(el:ElementRef) {
            this.el = el;
        }
        ngOnInit() {
            //$(function(){ console.log('Hello'); }); <--TEST JQUERY
            //check if your plugins are loading
            //$().mousewheel) ? console.log('mousewheel loaded') : console.log('mousewheel not loaded');
            //$().mCustomScrollbar) ? console.log('mCustomScrollbar loaded') : console.log('mCustomScrollbar not loaded');
    
            $(this.el.nativeElement).mCustomScrollbar({
                autoHideScrollbar: false,
                theme: "light",
                advanced: {
                    updateOnContentResize: true
                }
        });
    
  5. Include directive in ngModule and apply in your component's template

    //app.module.ts
    import {ScrollbarDirective} from "./shared/UI/scrollbar.directive";
    import {AppComponent} from "./app.component";
    
    @NgModule({
    ...
        declarations: [
            AppComponent,
            ...,
            ScrollbarDirective
        ],
    ...
    })
    export class AppModule{}
    

    //app.component.ts
    @Component({
        selector: 'dashboard',
        templateUrl: `
            <scrollbar>
                <div *ngFor="let thing of things">thing</div><br/>
            </scrollbar>
        `
    })