So you want to use a third-party library like jQuery or Lodash in your Angular 2+ project, but not sure how? Here’s the step by step breakdown, taking Lodash as the example:
First start by installing Lodash itself in your project with npm:
$ npm install --save lodash
Or with Yarn:
$ yarn add lodash
You can search for the available typings for your library using this tool: TypeSearch.
In our case, the typings for Lodash are packaged as @types/lodash, so we can install them with npm:
$ npm install --save @types/lodash
Or with Yarn:
$ yarn add @types/lodash
Now Lodash is ready to be imported in your components with an import statement like this:
import * as _ from "lodash";
Notice how we import everything from Lodash and give it an alias of _, so that it can be used as usual.
You can now test that Lodash is working with something like the following in your component class:
fruits = ['bananas', 'oranges', 'strawberries', 'kiwis'];
If you open your browser and check the console, you’ll see that _.chunk worked and the console logs our two arrays.
Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in our Questions & Answers section, find tutorials and tools that will help you grow as a developer and scale your project or business, and subscribe to topics of interest.
Sign up