1

I need to create a base class which consists of common logic and to Inherit it and create multiple components. While checking in the internet I found two ways of doing that.

  1. Use base class as a Pure typescript class and use @component decorator in inherited components.
  2. Use base class with @component with empty template and use @component in inherited components too.

It's appreciated if you can illustrate the correct way of achieving this.

2
  • 1
    Did you see this answer? Does that not work for you? Commented Mar 22, 2021 at 8:59
  • @RobbyCornelissen Commented Mar 22, 2021 at 9:12

2 Answers 2

2

There are several ways of achieving this depending on your use case. For simplicity, you can just create a base component class with a constructor and provide the dependencies from the inheriting or subclass via the super call e.g

export class BaseComponent {
    constructor(modal: ModalService) {
    }
    
    /*
        Implement code and logic common to inheriting components
     */
}

You can now extend this class from your component by injecting the modal service into the child class on passing onto 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)
    }
}

You can annotate your base component with the @Component angular decorator with an empty template to make your base component life cycle aware

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks ! Pretty useful to manage all rxjs and pipes ;)
0

No longer required. Now you can inherit from the base class and never have to supply the service.

import { Component, inject, OnInit } from '@angular/core';
import { MyServiceService } from '../my-service.service';

@Component({
  selector: 'app-my-base-component',
  templateUrl: './my-base-component.component.html',
  styleUrls: ['./my-base-component.component.css']
})
export class MyBaseComponentComponent implements OnInit {
  private service:MyServiceService;
  constructor() {
    this.service = inject(MyServiceService);
  }
  get alive(){
    return this.service.alive;
  }

  ngOnInit() {
  }

}
import { Component, OnInit } from '@angular/core';
import { MyBaseComponentComponent } from '../my-base-component/my-base-component.component';

@Component({
  selector: 'app-my-template',
  templateUrl: './my-template.component.html',
  styleUrls: ['./my-template.component.css']
})
export class MyTemplateComponent extends MyBaseComponentComponent {

  constructor() {
    super();
   }

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.