我有一个小问题,我需要在我的 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/