Use custom javascript / third party javascript inside a provider


#1

hello,

im having a problem to use custom javascript / a third party javascript library inside a provider! how do i realize that?

the javascript file is not listed in the npm repository, so this seems not to work: https://mhartington.io/post/ionic2-external-libraries/

this solutions seems to work inside page but not inside a provider: https://stackoverflow.com/questions/43833895/how-to-include-js-file-in-ionic-3

best regards


#2

was the 3rd party an NPM package?


#3

What’s the error you are getting?


#4

as mentioned above: the 3rd party is not in the npm repository, so no it is no npm package


#5

when loading the library in my index.html (as https://stackoverflow.com/questions/43833895/how-to-include-js-file-in-ionic-3 suggest) and i try to use it in my provider, im getting the message that the object i want to use is not defined when i try to access it… in a page component it works without problems…

i guess the problem is that the provider is initialized before the application is bootstrapped completly… im injecting the service in my app.components.ts (main class) in the constructor, i think the issue might be that this way the provider is initialized before the index.html is created, which loads the library


#6

If that’s the issue, you can probably just call the provider after the platform is ready.
platform.ready() => this.providerName.methodName();

Edit: Also, don’t put anything in the provider’s constructor or ngOnInit. If you need to initialize the provider, put that code in an initializeProvider() method, and call that method after the platform is ready.