【问题标题】:How to show and hide page in side menu IONIC2如何在侧边菜单 IONIC2 中显示和隐藏页面
【发布时间】:2017-07-13 08:13:10
【问题描述】:

我想在 ionic2 中创建侧边菜单。但是,我不知道如何在菜单中显示和隐藏选项。 我想在登录前和登录后在菜单中显示一些选项。

Before login             After login
---------                ---------
Home                     Home
Login                    Myprofile
                         Logout

app.ts

pages: Array<{title: string, component: any}>;
    pagess: Array<{title: string, component: any}>;

    this.pages= [
          { title: 'Home', component: HomePage},
          { title: 'Login', component: LoginPage}
        ];

    this.pagess = [
          { title: 'Home', component: HomePage},
          { title: 'Myprofile', component: MyprofilePage},
          { title: 'Logout', component: HomePage}
        ];

enableMenu(loggedIn: boolean) {
    this.menu.enable(loggedIn, 'loggedInMenu');
    this.menu.enable(!loggedIn, 'loggedOutMenu');
  }

我不知道如何设置 enableMenu

app.html

<ion-menu id="loggedOutMenu" [content]="content">
  <ion-header>
    <ion-toolbar>
      <ion-title>Menu</ion-title>
    </ion-toolbar>
  </ion-header>

  <ion-content>
    <ion-list>
      <button menuClose ion-item *ngFor="let p of pages" (click)="openPage(p)">
        {{p.title}}
      </button>
    </ion-list>

  </ion-content>
</ion-menu>

<ion-menu id="loggedInMenu" [content]="content">
  <ion-header>
    <ion-toolbar>
      <ion-title>Menu</ion-title>
    </ion-toolbar>
  </ion-header>

  <ion-content>
    <ion-list>
      <button menuClose ion-item *ngFor="let p of pagess " (click)="openPage(p)">
        {{p.title}}
      </button>
    </ion-list>

  </ion-content>
</ion-menu>

<ion-nav [root]="rootPage" #content swipeBackEnabled="false"></ion-nav>

login.ts(登录在 myphpfile 中检查)

login() {
       this.api.getLogin(this.username, this.password).subscribe(
   data => {

     let loader = this.loadingCtrl.create({
      content: "Please wait...",
      duration: 1000
    });
    loader.present();
    this.navCtrl.setRoot(HomePage);
   },
   err =>  {

    let alert = this.alertCtrl.create({
      title: 'Warning!',
      subTitle: 'incorrect!',
      buttons: ['OK']
    });
    alert.present();
  }
  );

}

【问题讨论】:

  • 如果您成功回拨,如果您没有登录,请查看页面,然后不要显示使用*ngIf 来实现此目标
  • 登录前sidemenu有什么用?
  • @varunaaruru 登录前setroot=home 和侧边菜单有home login 登录成功后setroot=home 和侧边菜单有home myprofile logout
  • @MohanGopi 你有我的例子T-T
  • 我想说的是,在用户登录或注册后保留菜单是一个好习惯,因为在这些页面中将不会使用侧边菜单。如果我错了,请纠正我跨度>

标签: angular typescript ionic-framework ionic2


【解决方案1】:

您可以通过订阅/收听loggedIn 事件来做到这一点。现在,这与您的正常登录不同。您需要检查您是否已登录。

您的 app.html 代码将保持如下:

<ion-menu [content]="content">
  <ion-header>
    <ion-toolbar>
      <ion-title>Menu</ion-title>
    </ion-toolbar>
  </ion-header>

  <ion-content>
    <ion-list>
      <button menuClose ion-item *ngFor="let p of pages" (click)="openPage(p)" *ngIf='p.isVisible'>
        {{p.title}}
      </button>
    </ion-list>

  </ion-content>
</ion-menu>

现在这是app.ts 的主要变化。

pages: Array<{title: string, component: any, isVisible: boolean}>;

constructor(){
  this.api.isLoggedIn().subscribe(loggedIn => {
    this.pages= [
      { title: 'Home', component: HomePage, isVisible: true},
      { title: 'Login', component: LoginPage, isVisible: !loggedIn},
      { title: 'Myprofile', component: MyprofilePage, isVisible: loggedIn},
      { title: 'Logout', component: LogoutPage, isVisible: loggedIn}
    ];
  });
}

现在,您的isLoggedIn() 是一个方法,当登录状态发生变化时,它会返回一个 Observable。它的数据是boolean。你可能已经在你的 API 中有这个,就像 firebase 一样,或者你需要维护/编写它。让我知道你在使用什么,我会更新我的答案。

【讨论】:

  • 我不使用 Firebase。我用mysql检查登录,你想看什么?对不起,我的英语和编程不好。 T_T
  • 您的 SQL 托管在哪里?您是否使用 HTTP GET/POST 调用从中获取值?无论如何,没关系。您也可以在本地维护登录信息。
  • 是的,我使用 HTTP GET 调用从中获取值。可以使用storage 维护登录信息吗?我想举例
  • 好的。我正在执行相同的操作。很快就会更新你。
  • 嘿,我找到了答案。将很快更新我的答案。您需要使用来自ionic-angularEvents。答案其实就在github.com/driftyco/ionic-conference-app
猜你喜欢
  • 1970-01-01
  • 2017-08-05
  • 2021-10-30
  • 1970-01-01
  • 2018-01-24
  • 2016-08-31
  • 1970-01-01
  • 2011-12-29
  • 1970-01-01
相关资源
最近更新 更多