So I saw that if you want to access some data globally in the App, you should do so through an @Injectable service that is declared in app.js and then you use it everywhere else, like so:
app.js:
@App({
template: '<ion-nav [root]="rootPage"></ion-nav>',
providers: [[CropModel],[SyncModel],[AuthService]],
config: {}
})
export class MyApp {
static get parameters() {
return [[Platform],[CropModel],[SyncModel],[AuthService]];
}
constructor(platform,crops,syncs,auth) {
this.cropStorage = new CropModel();
this.syncModel = new SyncModel();
this.auth=auth;
this.rootPage = NoSyncStart;
platform.ready().then(() => {
// Okay, so the platform is ready and our plugins are available.
// Here you can do any higher level native things you might need.
StatusBar.styleDefault();
});
}
}
But this results in an error as soon as I load it up, where there’s no provider for NavController on app.js; there shouldn’t be, I’m aware, but then what’s up with the error?
I believe that you have a mistake in the declaration of the providers - unlike with static get parameters() it should be a simple (one-dimensional) array, i.e. change it this way:
Ok, how about removing CropModel and SyncModel from the providers? From this code sample it looks like you’re using them only in app.js and you’re not even using them via DI. Update your code like this and see what happens then:
It seems that if I list any providers in app.js, I get the NavController error; everything works fine if I then proceed to declare the providers in the pages they’re used in, but then they’re not global, correct?
Even only with AuthService as a single provider there? Does it work if you move the AuthService from there to any page that uses it (yes, then a separate instance of the service is created for each page)? Aren’t there any other errors suggesting where the problem might be?
Yes, if I have even a single provider listed on the @App, then it throws that NavController error; in the stacktrace it says DI Exception followed by several app.bundle.js lines.
If I move the services only to the pages where they’re used, then they work, for that page.