Ionic 2 - Sidemenu中的Logout选项卡

Non*_*ono 2 typescript ionic-framework ionic2 ionic3 angular

如何在离子2侧面菜单中添加Logout选项卡?如果您已登录,则表明您正在使用DashboardPage.现在我有一个菜单项"Logout",它只是带我到主页.这是app.component.ts中包含所有菜单项的pages数组:

this.pages = [
    {title: 'Dashboard', component: DashboardPage},
    ...
    {title: 'Logout', component: HomePage}
];
Run Code Online (Sandbox Code Playgroud)

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

编辑:

这是openPage函数:

openPage(page) {
    this.nav.setRoot(page.component);
}
Run Code Online (Sandbox Code Playgroud)

seb*_*ras 10

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

this.pages = [
    {title: 'Dashboard', component: DashboardPage},
    ...
    {title: 'Logout', component: null}
];
Run Code Online (Sandbox Code Playgroud)

然后在你的方法中:

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