Reputation: 12015
There is a component <app-tabs-components>
.
It gets data from service. There is variable show: boolean
in this service to show/hide component on the page.
@Injectable({ providedIn: "root" })
export class TabService {
show = false;
showHide(status: boolean) {
this.show = status;
}
get() {
return this.show;
}
}
Problem is that I need to call tabService.showHide(false)
in every component where I want to hide component <app-tabs-components>
.
Component <app-tabs-components>
always should be closed if it lost focus, not active area.
In my case I am breaking DRY principle.
For example, when user activates any component I must do:
ngOnInit() {
tabService.showHide(false)
}
So this part of code is repeated ine each component where I wan to hide tabsComponent
Upvotes: 0
Views: 44
Reputation: 1973
Create a Base Class and in constructor of this class call the your service.
export class BaseComp {
public IsHide: boolean;
constructor() {
this.IsHide = tabService.showHide(false);
}
}
Inherit this BaseComp in different Components.
export class YourComponent extends BaseComp implements OnInit {
constructor() {
super(); // calls the base constructor
console.log(this.IsHide); // this IsHide is of Base class
}
ngOnInit() {
}
}
Hope this helps !!
Upvotes: 1