Tutorial

Using Third-Party Libraries in Angular Apps

Published on November 1, 2016
Default avatar

By Alligator.io

Using Third-Party Libraries in Angular Apps

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

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:

1. Installing Lodash

First start by installing Lodash itself in your project with npm:

$ npm install --save lodash

Or with Yarn:

$ yarn add lodash

2. Install the TypeScript type definitions

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

3. Import

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.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about us


About the authors
Default avatar
Alligator.io

author

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
Leave a comment


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel