I am using the sidemenu template to start my application. I want to add a button to sidemenu so that the user can run logout alert modal to confirm the logout. Here is my code:
app.component.ts:
import { Component, ViewChild } from '@angular/core'; import { Nav, Platform } from 'ionic-angular'; import { StatusBar, Splashscreen } from 'ionic-native'; import { Home } from '../pages/home/home'; import { Profile } from '../pages/profile/profile'; import { Logout } from '../pages/logout/logout'; @Component({ templateUrl: 'app.html' }) export class MyApp { @ViewChild(Nav) nav: Nav; rootPage: any = Home; pages: Array<{title: string, component: any}>; constructor(public platform: Platform, public logout:Logout) { this.initializeApp();
app.html:
<ion-menu [content]="content"> <ion-content> <ion-list> <button menuClose ion-item *ngFor="let p of pages" (click)="openPage(p)"> {{p.title}} </button> <button ion-item (click)="logoutApp()"> Log Out </button> </ion-list> </ion-content> </ion-menu> <ion-nav [root]="rootPage" #content swipeBackEnabled="false"></ion-nav>
app.module.ts:
import { NgModule } from '@angular/core'; import { IonicApp, IonicModule } from 'ionic-angular'; import { MyApp } from './app.component'; import { Home } from '../pages/home/home'; import { Profile } from '../pages/profile/profile'; import { Logout } from '../pages/logout/logout'; @NgModule({ declarations: [ MyApp, Home, Profile, Logout ], imports: [ IonicModule.forRoot(MyApp) ], bootstrap: [IonicApp], entryComponents: [ MyApp, Home, Profile, Logout ], providers: [] }) export class AppModule {}
logout.ts:
import { Component } from '@angular/core'; import { AlertController } from 'ionic-angular'; @Component({ template: `` }) export class Logout { constructor( public alertCtrl: AlertController ) { } presentConfirm() { let alert = this.alertCtrl.create({ title: 'Confirm Log Out', message: 'Are you sure you want to log out?', buttons: [ { text: 'Cancel', role: 'cancel', handler: () => { console.log('Cancel clicked'); } }, { text: 'Log Out', handler: () => { console.log('Logged out'); } } ] }); alert.present(); } }
Based on my knowledge, this should be enough. However, I received an error message:
45EXCEPTION: Error in. / MyApp class MyApp_Host - built-in template: 0: 0 caused by: No provider to exit the system!
But why do we need a provider here? Is there something I missed?