Angular |有没有办法导致 *ngIf 只运行一次?

标签 angular typescript permissions

我有一个小问题,我需要在我的 Angular 应用程序中应用 Angular 色验证。 该应用程序由多个页面构建,每个页面包含许多组件。 用户权限是在组件级别授予的,因此遗憾的是我不能仅使用路由保护来授予完整页面,我必须使用简单的 *ngIf 为每个组件添加验证:

     <results-component 
          *ngIf="auth.hasPermission('driver_productivity')"
          [format] = "driver_productivityFormat" 
          [IsFullWidth] = "IsFullWidth"
          (customEvent)="showPrint($event)">
     </results-component>

auth.service.ts:

hasPermission(permissionName){
if (!this.tokenPayload){
  this.getToken().subscribe(t=>{this.tokenPayload=decode(t.getValue())});
  if (this.tokenPayload.roles && this.tokenPayload.roles.length>0 ){
    this.permissions={};
    for (let i=0;i<this.tokenPayload.roles.length;i++){
      this.permissions[this.tokenPayload.roles[i]]=true
    }
  }
}
return this.permissions[permissionName]

}

但由于我每个页面有多个组件,因此此验证会累积太多时间,因此它成为一个滥用解决方案。

[Violation] 'setInterval' handler took 150ms zone.js:1755 [Violation] 'click' handler took 318ms zone.js:2279 [Violation] 'requestAnimationFrame' handler took 84ms zone.js:1755 [Violation] 'click' handler took 261ms

有没有办法让 *ngIf 只运行一次?组件何时首次加载?

最佳答案

Is there a way to make *ngIf run just once? when the component is first loaded?

是的。将相应的逻辑移至组件的 ngOnInit(...) 方法。因此,当组件加载时,它仅执行一次

关于 Angular |有没有办法导致 *ngIf 只运行一次?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59017309/

相关文章:

html - PrimeNG 表 [rowHovered] 不适用于设置了单元格背景的单元格

jquery - 如何在我的 Angular 应用程序中替换 jquery?

android - Ionic 应用程序以 API 级别 25 为目标,并且必须至少以 API 级别 26 为目标

javascript - 在提交时将选中的复选框的值保存在数组中 Ionic

android - 在Android上获取 "signature"权限级别

php - nginx 本地网络服务器的所有权和权限

angular - 在angular4中重新加载具有不同路由参数的组件

angular - 功能模块不导出组件

javascript - Typescript文件编译完成后为什么要执行ESlint?

ios - iOS 11 中的照片捕获权限问题