Multi instance of platform service

I have an Angular Module that imports the Ionic Module and hold a set of custom services and components I use across my apps. It is a separate project and is loaded as a npm module.

In my Ionic app project I use this module as an imported module.

This module has some functionality that needs to know about the app’s Ionic platform service, but when I inject the platform service in one of the module components, it is a different instance than the one in the app.

I think this happens because of the double import of the Ionic module, both in my module project and in my app. In my app I pass the root component in the forRoot method, but in the module I just import it (because there is no root component).

Could any one tell me how can I force the module project get the app’s instance of the platform service? or where is the right place to pass that instance to the module?

Hello, I’m really a noob so I would recommend more expert advice, because of your complex issues. Still, I think of 2/3 ideas:

  1. transactions: transactions are made so that records are never double-crossed, even across many people/things recording at the same place AND at the same time. This could help you, if you definitely don’t need any debug and the issue is about cross recording (several ips, clients, writing at the same time on non-concurrent deal).
  2. debugging: your issue seems quite complex to me. To that end, make sure you have Observables on each end of the issue (eg if button (click)=“testIt” and in constructor some kind of console.log either on object or arrays.
  3. don’t mix @injectable and observable, this will result in errors in Ionic 2

Oops, you can also force anything by the app"s instance or module in Ionic yes (go edit app.module.ts or app.component.ts). Both located in "your ionic project name/src/app directory or folder

If you don’t know how to formally make a Ionic project, just use the CLI, ionic start myBag, it will create the directory structure and basic files.

Hope it gives you hints :slight_smile:

This is a tricky one. I think the fundamental problem is that Platform is not a typical injectable. It is generated via a factory method, and that might explain what you are seeing.

There are workarounds involving dealing with the AppModule injector such as that described in this SO discussion, but if I were in your position and was going to bother with that AppInjector singleton, I would just try to leverage that idiom to pass the Platform instance outside of the ordinary DI band:

class PlatformHolder {
  private static _instance: PlatformHolder;
  platform: Platform;
  static getInstance(): PlatformHolder {
    if (!PlatformHolder._instance) { PlatformHolder.instance = new PlatformHolder(); }
    return PlatformHolder._instance;
  }
}

class AppComponent {
  constructor(plat: Platform) {
    let ph = PlatformHolder.getInstance();
    ph.platform = plat;
  }
}

class MeanwhileInDependentModule {
  needPlatform(): Platform {
    return PlatformHolder.getInstance().platform;
  }
}
1 Like

it worked perfectly! thank you