Expert wanna be
Expert wanna be

Reputation: 10624

Usage underscore library in Angular2

For now, I need to import the underscore library in each component.

But I want to import that only once in my root component which is AppComponent.

If I import underscore only in AppComponent, and use in sub-components, I go an error

ReferenceError: _ is not defined

import * as _ from 'underscore';

@Component({
...
})
export class AppComponent { }

How can I make it works?

Upvotes: 0

Views: 95

Answers (1)

Oliver Cooke
Oliver Cooke

Reputation: 1059

Importing the things you are going to use within a component is the way it has to be done.

Think about the way you declare an injectable service. You declare the service in a module then that becomes injectable within that area of the app, however you still have to import the service class into the component.

If possible set up a generic service just for underscore, and pass your values into it. However you will still need to declare that service on each page you use it.

Upvotes: 2

Related Questions