alearg
alearg

Reputation: 685

Are multiple imports of jQuery (and plugins) really necessary?

In view A I need a jQuery plugin so I do this:

import * as jQuery from 'jquery';
import plugin from 'path/of/plugin';

In view B I need just JQuery so I do this:

import * as jQuery from 'jquery';

If I load view A, then view B, then switch back to A, the plugin seems to be lost. Do I need to load jQuery on every view that needs it or should I import it globally from some main point? If so, how should I go about it?

--- UPDATE ---

Turns out the plugin does not get lost, it was a bundling / exporting issue I did not realize I was having.

Still, the aurelia docs do not seem to provide a clear recommendation on how / where / when to import jQuery and assorted plugins. For example, even jQuery itself, other people import it like this:

import * as jQuery from 'jquery';

and other like this:

import $ from 'jquery';

Do both ways amount to the same thing?

Upvotes: 2

Views: 498

Answers (2)

alearg
alearg

Reputation: 685

For anyone interested in this: There is an "aurelia way" after all! Have a look here.

Upvotes: 0

Fabio
Fabio

Reputation: 11990

To import jQuery, this is enough:

import $ from 'jquery';

To import a Jquery Plugin, that depends on what it exports. Usually, plugins do not export anything. So, you just have to import the file:

import 'path/of/plugin';
//now you can $('#something').myPluginFunction();

To understand more about importing classes, functions, etc, you should read ES6 spec for importing. Just like @Callum Linington said in his comment.

In my opinion, there is no specific rule for Aurelia, neither "Aurelia way" of importing. The importing mechanism is purely Javascript.

Helpful links

https://developer.mozilla.org/en/docs/web/javascript/reference/statements/import

https://babeljs.io/docs/learn-es2015/#modules

Hope this helps!

Upvotes: 2

Related Questions