简体   繁体   English

如何执行注销触发器并执行ngIf身份验证检查-Angular 4和Ionic 3

[英]how to execute logout trigger and do ngIf authentication check - Angular 4 and Ionic 3

I have 2 problems here: 我在这里有2个问题:

I am not sure how to attach below Logout function to Logout Button , as you can see I am populating pages using *ngFor Directive. 我不确定如何将“注销”功能下面的内容附加到“注销按钮” ,如您所见,我正在使用* ngFor Directive填充页面。

I have (click)=openPage(p) but for Logout page it should execute Logout function instead of opening a page. 我有(click)= openPage(p),但是对于注销页面,它应该执行注销功能,而不是打开页面。

Also, Angular does not allow multiple *directives (for example: *ngIf and *ngFor) on the same element so how do I check for isAuthenticated to hide or show menu buttons. 另外, Angular在同一元素上不允许多个*指令(例如:* ngIf和* ngFor),因此如何检查isAuthenticated以隐藏或显示菜单按钮。 the tricky bit is that if user is not authenticated then I want to show 3 pages : login, register, contact and if authenticated show all other. 棘手的一点是,如果用户未通过身份验证,那么我想显示3页:登录,注册,联系以及是否通过身份验证显示所有其他页面。

app.component.ts app.component.ts

this.pages = [
  { title: 'Dashboard', component: DashboardPage, icon: 'stats'  },
  { title: 'Analytics', component: TabsPage, icon: 'analytics'  },
  { title: 'Portfolio', component: ProtabsPage, icon: 'images'  },
  { title: 'Profile', component: PtabsPage, icon: 'person'  },
  { title: 'Customize', component: SettingsPage, icon: 'options'  },
  { title: 'Contact', component: ContactPage, icon: 'call'  },
  { title: 'Logout', component: DashboardPage, icon: 'log-out'  },
  { title: 'Register', component: RegisterPage, icon: 'person-add'  },
  { title: 'Login', component: LoginPage, icon: 'log-in'  }
  ];
this.activePage = this.pages[1];

Logout() {
    this.authService.logout();
}

authService.ts authService.ts

logout(): void
{
    localStorage.removeItem('currentUser');
    this.isLoggedin = false;
    //Redirect to Login Page
}

app.html app.html

  <button padding ion-item class="menu-btn" text-center *ngFor="let p of pages" [class.activeHighlight]="checkActive(p)" (click)="openPage(p)">
    <ion-icon  name="{{p.icon}}" ></ion-icon>
    <h4>{{p.title}}</h4>
  </button>

You can use ng-container with *ngIF 您可以将ng-container*ngIF

 <ng-container *ngFor="let p of pages">
<button  *ngIf="isLoggedin && p.title ==='Logout'" padding ion-item class="menu-btn" text-center  [class.activeHighlight]="checkActive(p)" (click)="Logout (p)">
    <ion-icon  name="{{p.icon}}" ></ion-icon>
    <h4>{{p.title}}</h4>
  </button>
<button  *ngIf="!isLoggedin"  padding ion-item class="menu-btn" text-center  [class.activeHighlight]="checkActive(p)" (click)="openPage(p)">
    <ion-icon  name="{{p.icon}}" ></ion-icon>
    <h4>{{p.title}}</h4>
 </button>
</ng-container>

You can choose to logical management inside the class. 您可以选择在班级内部进行逻辑管理。 Instead of fixed open() method, put a method which will call corresponding method (open/logout, etc..) depending on the component that used it. 代替固定的open()方法,请放置一个方法,该方法将根据使用该方法的组件来调用相应的方法(open / logout等)。 You can even add more checks here, like if it's logged or not. 您甚至可以在此处添加更多检查,例如是否已记录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM