Reputation: 3053
I need to import jQuery in my Angular 7 application and I noticed that where are two ways to do this.
scripts
property of angular.jsonimport 'jquery';
What is the difference between these approaches?
Upvotes: 0
Views: 440
Reputation: 5074
First I must say that mixing JQuery with angular is in most cases a bad idea, So I hope You have a good reason for it. About your question
When importing a file using scripts
property of angular.json, You do exactly that - importing the entire file and running it's code. On angular, the will be done prior to the execution of your angular app code.
On the other hand, when importing a module (after installing it using a package manager like npm), You can import only the modules you need, which usually means less code in being executed by the browser. I wrote usually, because when using JQuery you most likely import the entire library in anyway.
I would always prefer installing using a package manager when possible, since:
Upvotes: 2
Reputation:
Makes the script global (like placing a script tag in index.html)
Makes use of modules, which avoids global scope pollution, and lets tools like Webpack optimise the bundling process, e.g. by “tree-shaking”.
Upvotes: 2