Ionic 2 - 侧边菜单中的注销选项卡

Ionic 2 - Logout Tab in Sidemenu

如何在我的 ionic 2 侧边菜单中添加注销选项卡?如果您已登录,则您位于 DashboardPage 上。现在我有一个菜单项 "Logout",它将我带到主页。这是 app.component.ts 中包含所有菜单项的页面数组:

this.pages = [
    {title: 'Dashboard', component: DashboardPage},
    ...
    {title: 'Logout', component: HomePage}
];

但现在我需要在注销后添加逻辑,而不仅仅是页面切换。当我单击“注销”选项卡而不是仅转到主页时,是否可以调用函数 logout()?

编辑:

打开页面函数如下:

openPage(page) {
    this.nav.setRoot(page.component);
}

您可以在注销选项中将组件设置为空

this.pages = [
    {title: 'Dashboard', component: DashboardPage},
    ...
    {title: 'Logout', component: null}
];

然后在你的方法中:

openPage(page) {
    if(page.component) {
        this.nav.setRoot(page.component);
    } else {
        // Since the component is null, this is the logout option
        // ...

        // logout logic
        // ...

        // redirect to home
        this.nav.setRoot(HomePage);
    }
}

我知道这个问题已经得到解答,但任何人都会觉得这很有用。

    this.pages = [
        {title: 'Dashboard', component: DashboardPage},
        {title: 'Logout', component: HomePage},
        {title: 'Share', component: ''}
    ];

    openPage(page) {

        switch (true) {

          case ((page.title == 'Logout')): {
            console.log('Clicked Logout button');
            // this.logout(); // call logout logic method
          }
              break;

          case ((page.title == 'Share')): {
            console.log('Clicked Share button');
            // this.share(); call share logic method
          }
              break;

          default: {
            this.nav.setRoot(page.component);
          }
              break;
      }

    }