That’s the shortest version of it, which I imagine none of you understand, so let me get a bit more into detail. First, I’d like to point out that I consider myself fairly new to Ionic and Angular, so there’s probably a bunch of terms etc. I don’t understand just yet Just keep that in mind when responding.
So, basic setup:
|-- app
| |-- app.component.ts
|-- pages
| |-- login
| |-- login.ts
|-- providers
|-- user-service.ts
Those are the files that matter, I believe. Quick explanation of the issue and then I’ll paste some of the code in here: The root page I am setting in the app.component.ts is the login page. The login page imports the UserService provider, which on launch does a quick check of the session information to determine whether or not to redirect you away from the login page. The same function is used to redirect you TO the login page, if the app says you are logged in but the session check says otherwise. That is where the error occurs, as soon as I add the following to my UserService
if(this.loggedIn) this.app.getRootNav().setRoot(LoginPage);
I get the following error:
Uncaught Error: Can't resolve all parameters for LoginPage: (NavController, LoadingController, AlertController, TranslateService, ?, SimpleStorage)
My assumption is that it’s because it loops somehow, and I have no idea how to get around it…
login.ts
import { Component } from '@angular/core';
import { NavController, LoadingController, AlertController } from 'ionic-angular';
import { TranslateService } from 'ng2-translate';
import { UserService } from '../../providers/user-service';
import { SimpleStorage } from '../../providers/storage-service';
@Component({
selector: 'page-login',
templateUrl: 'login.html'
})
export class LoginPage {
constructor(
public navCtrl: NavController,
public loadingCtrl: LoadingController,
public alertCtrl: AlertController,
private translate: TranslateService,
public userService: UserService,
public simpleStorage: SimpleStorage
) {}
}
This page is calling the login function in UserService
user-service.ts
import { Injectable } from '@angular/core';
import { App, LoadingController, AlertController, Platform } from 'ionic-angular';
import 'rxjs/add/operator/map';
import { TranslateService } from 'ng2-translate';
import { BackendService } from '../providers/backend-service';
import { SimpleStorage } from '../providers/storage-service';
import { NewsPage } from '../pages/news/news';
import { LoginPage } from '../pages/login/login';
@Injectable()
export class UserService {
public loggedIn: boolean = false;
constructor(
public app: App,
public platform: Platform,
public loadingCtrl: LoadingController,
public alertCtrl: AlertController,
public translate: TranslateService,
public backendService: BackendService,
public simpleStorage: SimpleStorage
) {
platform.ready().then(() => {
this.checkSession();
});
}
checkSession(silent: boolean = false) {
this.simpleStorage.getAsyncList(["user.sessid", "user.uid"]).then(
result => {
if(!result) {
let loader = this.loadingCtrl.create({
content: this.translate.instant("pages.login.logging_in"),
spinner: "crescent"
});
if(!silent) loader.present();
this.backendService.call(
{
**data removed**
}
).subscribe(
result => {
if(!silent) {
loader.dismiss();
this.loggedIn = true;
this.app.getRootNav().setRoot(NewsPage);
}
},
err => {
this.checkSessionFailed();
if(!silent) loader.dismiss();
let alert = this.alertCtrl.create({
title: this.translate.instant("providers.UserService.session_expired.title"),
subTitle: this.translate.instant("providers.UserService.session_expired.message"),
buttons: [this.translate.instant("general.confirm.ok")]
});
alert.present();
}
);
} else {
this.checkSessionFailed();
}
}
);
}
checkSessionFailed() {
this.simpleStorage.removeList(["user.fullname", "user.firstname", "user.lastname", "user.sessid"]);
if(this.loggedIn) this.app.getRootNav().setRoot(LoginPage);
this.loggedIn = false;
}
}
As already mentioned, as soon as I add in the 4th last line it gives me the error
I have removed parts of the code I decided weren’t necessary, if you need anything else just let me know
Thanks in advance!