我的问题类似于找到here
的问题但是我不确定它是否与他的情况相同,因为当我停止应用程序并使用离子服务重新启动时,错误有时会消失。该错误随机返回。我注意到当我在app.component.ts构造函数中使用调试器语句时,我可以绕过错误。任何帮助,将不胜感激。谢谢。
这是我的代码
app.component.ts
export class MyApp {
@ViewChild('content') nav: Nav;
rootPage: any = HomePage;
pages: Array<{ title: string, component: any }>;
constructor(public platform: Platform,
public statusBar: StatusBar,
public splashScreen: SplashScreen) {
this.initializeApp();
this.pages = [
{ title: 'Dashboard', component: DashboardPage },
];
debugger
}
initializeApp() {
this.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.
this.statusBar.styleDefault();
this.splashScreen.hide();
});
}
openPage(page) {
// Reset the content nav to have just this page
// we wouldn't want the back button to show in this scenario
this.nav.setRoot(page.component);
}
}
app.html
<ion-menu [content]="content">
<ion-header>
<ion-toolbar>
<ion-title>Menu</ion-title>
</ion-toolbar>
</ion-header>
<ion-content>
<ion-list>
<button menuClose ion-item *ngFor="let p of pages" (click)="openPage(p)">
{{p.title}}
</button>
</ion-list>
</ion-content>
</ion-menu>
<ion-nav #content swipeBackEnabled="false" [root]="rootPage"></ion-nav>
home.ts
export class HomePage {
token: string;
constructor(public navCtrl: NavController,
public menuCtrl: MenuController,
public storage: Storage) {
setTimeout(() => {
this.storage.get('_token').then((token) => {
this.token = token;
if (this.token && this.token.length > 0) {
this.navCtrl.setRoot(DashboardPage)
} else {
this.navCtrl.setRoot(LoginPage)
}
}).catch((error) => {
console.log(error);
this.navCtrl.setRoot(LoginPage)
});
}, 1000)
}
ionViewWillEnter() {
this.menuCtrl.enable(false);
}
ionViewWillLeave() {
this.menuCtrl.enable(true);
}
}
login.ts
export class LoginPage {
username: string;
password: string;
constructor(public navCtrl: NavController,
public navParams: NavParams,
public userProvider: UserProvider,
public storage: Storage,
public alertCtrl: AlertController,
public menuCtrl: MenuController) {
}
ionViewDidLoad() {
console.log('ionViewDidLoad LoginPage');
}
login() {
console.log("Logging in...", this.username, this.password);
var credentials = { 'email': this.username, 'password': this.password };
this.userProvider.login(credentials).then((validResponse: any) => {
console.log('success', validResponse);
var data = validResponse.data;
this.storage.set('_token', data.token);
this.navCtrl.setRoot(DashboardPage);
}).catch((errorResponse: any) => {
console.log('error', errorResponse);
var title = '';
var subTitle = '';
title = "Oops!"
if (errorResponse.status == 401) {
subTitle = "Invalid Username/Password";
} else {
var errorObject = errorResponse.error;
if (errorObject.errors) {
for (let error of errorObject.errors) {
subTitle += error + '. ';
}
} else {
subTitle = errorResponse.message;
}
}
this.presentAlert(title, subTitle);
});
}
presentAlert(title, subTitle) {
let alert = this.alertCtrl.create({
title: title,
subTitle: subTitle,
buttons: ['Ok']
});
alert.present();
}
ionViewWillEnter() {
this.menuCtrl.enable(false);
}
ionViewWillLeave() {
this.menuCtrl.enable(true);
}
}
的login.html
<ion-content padding>
<div class="login-holder">
<ion-img class="logo" width="120" src="../../assets/imgs/logo.png"></ion-img>
<br>
<ion-img class="hero" width="250" src="../../assets/imgs/restaurant.png"></ion-img>
<ion-input class="username" [(ngModel)]="username" type="text" placeholder="Phone or Email"></ion-input>
<ion-input class="password" [(ngModel)]="password" type="password" placeholder="Password"></ion-input>
<button class="login" ion-button full (click)="login()">Login</button>
</div>
</ion-content>
我的流程是这样的,HomePage是一个临时屏幕,然后进入登录页面, 在超时后,我将进入LoginPage或Dashboard Page,具体取决于他们是否已在本地存储中拥有令牌。从登录页面,我将用户带到仪表板页面。
由于我是角色和离子的新手,我甚至不确定我是否正确行事。我已经查看了堆栈溢出中的所有类似问题。
再次感谢您的时间。
答案 0 :(得分:0)
将您的Home.ts更改为:
export class HomePage {
token: string;
constructor(public navCtrl: NavController,
public menuCtrl: MenuController,
public storage: Storage) {
}
ionViewWillEnter() {
this.menuCtrl.enable(false);
}
ionViewWillLeave() {
this.menuCtrl.enable(true);
}
ionViewDidLoad() {
setTimeout(() => {
this.storage.get('_token').then((token) => {
this.token = token;
if (this.token && this.token.length > 0) {
this.navCtrl.setRoot(DashboardPage)
} else {
this.navCtrl.setRoot(LoginPage)
}
}).catch((error) => {
console.log(error);
this.navCtrl.setRoot(LoginPage)
});
}, 1000)
}
}
答案 1 :(得分:0)
环顾四周。我发现了这个问题。我家里缺少 @IonicPage 装饰器。添加后它得到了解决。