简体   繁体   English

如何在不使用@Input 选项的情况下使用自定义配置制作 Angular 组件通用?

[英]How to make an Angular component Generic with customized configuration without using @Input option?

Hello angular community,你好angular社区,

I want to ask about a good way to make an icon-component generic and avoid code duplication我想问一个使图标组件通用并避免代码重复的好方法

delete-icon-component:删除图标组件:

@Component({
  selector: 'app-delete-row-icon',
  template: `
    <style>
      button {
        background-color: Transparent;
        border: none;
        cursor: pointer;
        overflow: hidden;
        outline: none;
      }

      mat-icon {
        font-size: 18px;
      }
    </style>
    <button (click)="onClickDeleteIcon($event)">
      <mat-icon
        matTooltip="Unselect file"
        matTooltipPosition="below"
        class="material-icons-outlined"
        >{{ iconRegistry.REMOVE }}</mat-icon
      >
    </button>
  `,
  styles: [],
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class DeleteRowIconComponent implements ICellRendererAngularComp {
  params;
  disabled = false;
  iconRegistry = IconRegistry;

  agInit(params): void {
    this.refresh(params);
  }

  refresh(params): boolean {
    this.params = params;
    return true;
  }

  onClickDeleteIcon($event): void {
    const params = {
      rowData: this.params.data,
      rowIndex: this.params.rowIndex
    };
    this.params.onClick(params);
  }

remove-icon-component:删除图标组件:

import { ChangeDetectionStrategy, Component } from '@angular/core';
import { ICellRendererAngularComp } from 'ag-grid-angular';
import { IconRegistry } from '../../../icon-registry/icon-registry';

@Component({
  selector: 'app-remove-icon-render',
  template: `
    <style>
      button {
        background-color: Transparent;
        border: none;
        cursor: pointer;
        overflow: hidden;
        outline: none;
      }
      button :hover {
        background-color: #efeadf;
      }
      mat-icon {
        font-size: 18px;
      }
    </style>
    <button
      *ngIf="params?.data && !!params.data.objectId && !!params.data.objectPath"
      (click)="onClick($event)"
      [disabled]="params?.disabledGetter ? params.disabledGetter(params) : false"
      data-cy="icon-cell-Delete"
    >
      <mat-icon [matTooltip]="tooltip" matTooltipPosition="below" class="material-icons-outlined">
        {{ iconRegistry.MINUS }}
      </mat-icon>
    </button>
  `,
  styles: [],
  changeDetection: ChangeDetectionStrategy.OnPush
})

export class RemoveIconRenderComponent implements ICellRendererAngularComp {
  params;
  iconRegistry = IconRegistry;
  tooltip: string;

  agInit(params): void {
    this.params = params;
    if (this.params?.tooltipGetter) {
      this.tooltip = this.params.tooltipGetter(this.params);
    } else {
      this.tooltip = this.params?.tooltip;
    }
  }

  onClick($event): void {
    $event.stopPropagation();
    const params = {
      rowData: this.params.node.data
    };
    this.params.onClick(params);
  }

  refresh(params): boolean {
    this.params = params;
    return true;
  }

the only difference is the ICON and the logic behind agInit function唯一的区别是 ICON 和 agInit 背后的逻辑agInit

I cannot use the @Input feature, because I will use these components inside ts code and not HTML code this way:我不能使用 @Input 功能,因为我将在 ts 代码中使用这些组件,而不是 HTML 代码,这样:

  this.frameworkComponents = {
      deleteButtonRenderer: DeleteIconRenderComponent
    };

So any good recommended way to make one generic component that can accept any icon?那么有什么好的推荐方法可以制作一个可以接受任何图标的通用组件吗?

You could create an abstract RowIconComponent and extend from it.您可以创建一个抽象的 RowIconComponent 并从中扩展。 Something like this:像这样:

export abstract class IconComponent {
  params;
  iconRegistry = IconRegistry;
  tooltip: string;

  refresh(params): boolean {
    this.params = params;
    return true;
  }

  abstract onClick($event): void;
  abstract agInit(params): void;
}

And changed example:并更改了示例:

@Component({
  // ...
})
export class DeleteRowIconComponent extends IconComponent implements ICellRendererAngularComp {
  agInit(params): void {
    this.refresh(params);
  }

  onClick($event): void {
    const params = {
      rowData: this.params.data,
      rowIndex: this.params.rowIndex
    };
    this.params.onClick(params);
  }
}

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

相关问题 如何在应用程序中为 Angular Forms 制作带有表单控制输入的通用组件 - How to make generic component with form control input for Angular Forms in app 如何使用文件上传选项在angular2中进行简单的自定义拖放功能 - How to make simple customized drag and drop functionality in angular2 with file upload option Angular:在组件内部使用通用组件 - Angular: Using generic component inside a component 没有值的Angular 2组件输入 - Angular 2 component input without value 如何在 angular 8 中添加通用组件 - How to add generic component in angular 8 如何在不使用ng角度的情况下将输入值从父组件传递给子组件? - How to pass input value from parent component to child without using ng in angular? 反应式表单:如何使用 Angular 上的 FormControl 将输入值捕获到组件内的变量中,而不使用 [(ngModel)]? - Reactive form: How to capture the input value into a variable within component using FormControl on Angular WITHOUT [(ngModel)]? 在没有 Input() 方法的另一个 Angular 组件中使用变量 - Using variable in another Angular component without Input() method 如何使用formcontrolname制作输入组件 - How to make input component using formcontrolname 在 Angular 中测试组件时如何正确指定哪个组件具有泛型类型的输入数据 - How to correctly specify when testing a component in an Angular that which component has input data in generic type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM