pjay
pjay

Reputation: 435

Renderer2 not working while inheritance in angular7

I need to access dom element and manipulate in other class
homeComponent.ts

   import{TableUtility} from "src/shared/table-utility"; 
   export class HomeComponent extends TableUtility{
   constructor(){super()}

homeComponent.html

<button (click)="toggleClass($event)"><img src="img.png"></button>

tableUtility.ts

import{Table} from "primeng/table";
import{Renderer2} from "@angular/core";

export class TableUtility{
constructor(public render:Renderer2)
togglClass(ev){
console.log(this.render.addClass(ev.target,"active");
}

Err:cannot read addClass of undefined

If I use it within home component I can read Renderer2. Can someone tell this with proper reason and how to solve this.

Upvotes: 1

Views: 296

Answers (1)

Julius Dzidzevičius
Julius Dzidzevičius

Reputation: 11000

You need to define dependencies in subclass constructor and pass them to superclass via super():

export class HomeComponent extends TableUtility{
constructor(public render:Renderer2){super(render)}

Upvotes: 2

Related Questions