Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ionic2 - add log out in sidemenu

Tags:

I am using sidemenu template to begin my app. I want to add a button in sidemenu for user to launch a logout alert modal to confirm logout. Here are 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();

    // used for an example of ngFor and navigation
    this.pages = [
      { title: 'Home', component: Home },
      { title: 'Profile', component: Profile }
    ];

  }

  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.
      StatusBar.styleDefault();
      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);
  }

  logoutApp(){ ///<-- call from static button
    this.logout.presentConfirm(); ///<-- call 
  }

}

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()">
      <!--Add this static button for logout-->
        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 sufficient. However I got an error:

45EXCEPTION: Error in ./MyApp class MyApp_Host - inline template:0:0 caused by: No provider for Logout!

But why do we need provider here? Is there something I missed out?

like image 905
sooon Avatar asked Oct 30 '16 14:10

sooon


1 Answers

Logout isn't a provider (it's a component), yet you're trying to inject it into MyApp. From the looks of it, it doesn't look like your intention is really to make the Logout a component. In which case, you should instead replace the @Component() with @Injectable()

import { Injectable } from '@angular/core';

@Injectable()
export class Logout {
}

Then remove it from the @NgModule.declarations and @NgModule.entryComponent, and add it to the @NgModule.providers

@NgModule({
  declarations: [
    // Logout
  ],
  entryComponents: [
    // Logout
  ],
  providers: [
    Logout
  ]
})
class AppModule {}

If Logout is supposed to be a component and you want to be able to access a method from it inside MyApp, what you should do instead is create a service that can be injected both into the Logout and MyApp, that can make use the service functionality to present the alert.

like image 72
Paul Samsotha Avatar answered Sep 22 '22 16:09

Paul Samsotha