ionic2 - 在sidemenu中添加注销

soo*_*oon 5 typescript ionic2 angular

我正在使用sidemenu模板开始我的应用程序.我想添加一个按钮,sidemenu以便用户启动logout警报模式以确认注销.这是我的代码:

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 
  }

}
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

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 {}
Run Code Online (Sandbox Code Playgroud)

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

}
Run Code Online (Sandbox Code Playgroud)

根据我的知识,这应该足够了.但是我收到了一个错误:

45EXCEPTION:./ MyApp类中的错误MyApp_Host - 内联模板:0:0引起:没有提交注销!

但为什么我们需要provider这里呢?有什么我错过了吗?

Pau*_*tha 2

Logout不是提供者(它是一个组件),但您正在尝试将其注入到MyApp. 从表面上看,你的意图似乎并不是真的要制作一个Logout组件。在这种情况下,你应该用replacewith@Component()来代替@Injectable()

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

@Injectable()
export class Logout {
}
Run Code Online (Sandbox Code Playgroud)

然后将其从@NgModule.declarations和中删除@NgModule.entryComponent,并将其添加到@NgModule.providers

@NgModule({
  declarations: [
    // Logout
  ],
  entryComponents: [
    // Logout
  ],
  providers: [
    Logout
  ]
})
class AppModule {}
Run Code Online (Sandbox Code Playgroud)

如果Logout 应该是一个组件,并且您希望能够从其内部访问方法MyApp,那么您应该做的是创建一个可以注入到 和 中的服务LogoutMyApp该服务可以使用服务功能来呈现警报。