Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do i use @types/jqueryui along with angular2 (typescript)

I would like to use jqueryui in my angular application

I can import jquery like this

import * as $ from 'jquery';

But how do i import '@types/jqueryui' from https://www.npmjs.com/package/@types/jqueryui

Since jqueryui is an interface, i am not able to import it

How i am using jquery

 export class JqueryUIIntegrationComponent implements AfterViewInit{
        @ViewChild('jqueryElement') el:ElementRef;
        constructor() {
        }

    ngAfterViewInit() {
        console.log(this);
        $(this.el.nativeElement).slideUp(4000).slideDown(5000); // jquery function
        $(this.el.nativeElement).draggable(); //Since jqueryui is not imported this will not work
    }
}
like image 307
Jeba Prince Avatar asked Jan 24 '17 05:01

Jeba Prince


People also ask

How to install and use jQuery in angular with typescript?

Install and Use jquery in Angular with typescript jquery @types. To install jQuery type definations file in Angular projects use the following command. npm install --save jquery npm install --save @types/jquery. After successful installation you will see a folder inside the node_modules/@types with jQuery type defination files.

How to add jQuery to TypeScript?

This will add a jQuery version of Typescript with an extension same as of Typescript files. Drag and drop this “jquery.d.ts” file on the “demo.ts” file. This will add a reference to the JQuery library. Now, you are ready to use all the API’s of jQuery in your Typescript files.

How to avoid jQuery type defination errors in angular?

To avoid such kind of errors in Angular, we need to have type defination files for every third party library we use in typescript or angular projects in this case jQuery. Luckily we have jquery type definations are available as part of separate npm packages. To install jQuery type definations file in Angular projects use the following command.

How to import jQuery in angular?

If you use jquery @types no need to add jquery.min.js file reference in angular.json file. And in the Angular component file import the jquery as shown below. And if you hover over the $ element you will see the defination of jquery object as shown below. And we can also you give your own name for jQuery imports as shown below.


1 Answers

Work Around: (Not the solution through @types/jqueryui )

You can use @types/jqueryui for typing/autocomplete support.

Root HTML:

Import jquery and jquery UI js files

<script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"></script>

Component:

In your component declare

declare var $:any;

use it in class like this

export class JqueryUIIntegrationComponent implements AfterViewInit {
    @ViewChild('jqueryElement') el:ElementRef;
    constructor() {
    }

    ngAfterViewInit() {
        console.log(this);
        $(this.el.nativeElement).draggable(); //same old style
    }
}

Note: Any native support through @types/jqueryui (right way) is welcomed. please let me know if you find any solution.

like image 162
Jeba Prince Avatar answered Oct 06 '22 00:10

Jeba Prince