Sandrooco Sandrooco - 1 year ago 189
AngularJS Question

Angular2: declare vars for all components

I'm using a company-wide internally hosted js-library. I referenced the script in the index.html file. To get it working I used the "declare" keyword in my root component to initialize some stuff when the app loads:

declare const someInternalLibrary: any;

Now, in another component I'd like to use the same library to do some other stuff.
How can I use it without repeating the upper line of code? That wouldn't be too clean I think. Is there something like inheritance for child-components? I read something about declarations, as I'm very new to ng2 I don't really see the way through though.

Answer Source

You may utilize typings definition in your application to declare your internal library.

If you are using angular-cli there is a typings.d.ts file there you may add all custom declarations. even you may define all your functions from the internal library which will then give you intellisense.

To read more how to write Declaration file you may look here (Writing Declaration Files)

Hope this helps!!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download