If you need to achieve this, there are multiple approaches you can take depending on your specific scenario. One simple way is to create a base component class with a constructor where you can pass dependencies from the inheriting classes using the super call, like so:
export class BaseComponent {
constructor(modal: ModalService) {
}
/*
Add shared code and logic here that will be inherited by other components
*/
}
To implement this in your component, you can extend the base class and inject the modal service in the child class while passing it to the base class:
import {Component, OnInit} from '@angular/core';
import {BaseComponent} from './base.component';
@Component({
selector: 'app-child',
templateUrl: './child.component.html',
styleUrls: ['./child.component.scss'],
})
export class ChildComponent extends BaseComponent implements OnInit {
constructor(modal: ModalService) {
super(modal)
}
}
Additionally, you can use the @Component
Angular decorator to mark your base component, providing an empty template to ensure its life cycle is observed properly.