Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using window.plugins with Ionic 2 Typescript

Can you use window.plugins.xxx in an ionic 2 Typescript app?

I am getting an error Property 'plugins' does not exist on type 'Window' with the OneSignal plugin window.plugins.OneSignal.init()

like image 886
Bill Noble Avatar asked Jun 23 '16 19:06

Bill Noble


Video Answer


2 Answers

You could fix this by referencing plugins as an array property instead of object.

var OneSignalClient = window['plugins'].OneSignal.init()

Typescript doesn't check if array properties are defined on initial build so it safely passes inspection.

P.S. If you're not using Ionic but some other framework ( Angular 2 ), you won't have a plugins object in your window object, so you'll have to load OneSignal like this:

let OneSignalClient = window['OneSignal'] || [];

Hope this helps.

like image 95
Borislav Itskov Avatar answered Oct 06 '22 01:10

Borislav Itskov


You can call OneSignal directly without having to go through window. It looks like their documentation is a bit out of date.

OneSignal.init() should do the trick.

like image 41
Drakee510 Avatar answered Oct 06 '22 03:10

Drakee510