'Angular: Is there a way to stop a component from initializing it's view?
What I am trying to do is, if this.pageService.getForbidden() return true.
I don't want this component to load or initialized, is there a way to achieve this?
export class ViewRComponent extends ViewRulesParentComponent implements OnInit, AfterViewInit {
constructor( protected readonly pageService: PageService,
) {
if( this.pageService.getForbidden()){
// this.pageService.getForbidden() is true, I don't want this component to load
return;
}
}
}
Solution 1:[1]
If you got a parent component you can just use a <div> with *ngIf=pageService.getForbidden()
something like this:
parent.html
<div *ngIf=pageService.getForbidden()>
<view-r-component></view-r-component>
</div>
where <view-r-component> its your ViewRComponent component
i hope it works for you!
Solution 2:[2]
I don't know how your application is structured, but you can use Angular Guards to prevent the component to be loaded, something like CanActivate and CanLoad guards can help you.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Panken0 |
| Solution 2 | Rafael Andrade |
