2

I'm trying to use the plugin jquery tooltipster along with Angular CLI 1.5.5 and I'm having some difficulties. Basically, my code is the following:

import * as $ from 'jquery';
import 'jquery-tooltipster/js/jquery.tooltipster.min';

...

$(this.itemRef.nativeElement).tooltipster({
  position: "bottom"
});

Then, in the console, I get

jquery.tooltipster.min.js:1 Uncaught ReferenceError: jQuery is not defined

I understand that it's because "jquery-tooltipster" is trying to extend "jQuery" (the global one) and does not find it but then, I have no idea how to achieve that. I think that I could use ng eject to get the webpack.config file and use the ProvidePlugin, but I'd like not to eject the configuration and work with the angular-cli.json instead.

Is there a way to make a jQuery plugin works with ng-cli or must I eject the configuration?

1

1 Answer 1

1

There are several ways to do it. You can just includes the jQuery and plugin js files in your index.html or scripts sections in .angular-cli and then have that line in your code where you use jQuery

declare var $ : any;

Otherwise, you can try importing the jQuery type

npm install @types/jquery --save-dev

(you might need to extend the jQuery interface to add your plugin's signature though)

Sign up to request clarification or add additional context in comments.

3 Comments

Ah ! The part I was missing was the inclusion of the js script in the "script" part of "angular-cli.json". So I added "jQuery" and "jQuery-tooltipster" in there, then put declare var $ : any; where I'm using jQuery and it works well. Thanks ;-)
In my humble opinion, Typescript loses (almost) all of its purpose when we do the declare var $: any; thing. If using standalone jQuery, I would certainly go with installing its typings. The problem is: how to declare/import proper typings for the plugins?
You can declare your own interface I guess

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.