简体   繁体   English

当 inheritance 在 angular7 中时,渲染器 2 不工作

[英]Renderer2 not working while inheritance in angular7

I need to access dom element and manipulate in other class我需要访问 dom 元素并在其他 class 中进行操作
homeComponent.ts主页组件.ts

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

homeComponent.html主页组件.html

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

tableUtility.ts表格实用程序.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错误:无法读取未定义的 addClass

If I use it within home component I can read Renderer2.如果我在 home 组件中使用它,我可以读取 Renderer2。 Can someone tell this with proper reason and how to solve this.有人可以用适当的理由告诉这个以及如何解决这个问题。

You need to define dependencies in subclass constructor and pass them to superclass via super() :您需要在子类构造函数中定义依赖项并通过super()将它们传递给超类:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM