如何使用角管作为共享组件或共享模块

gih*_*ara 1 javascript angular angular6

我使用下面的函数作为管道,以获得下拉列表的唯一值。我需要在多个组件中使用它。我如何创建可重用组件以使用此功能。

 @Pipe({
  name: 'unique',
  pure: false
})

export class UniquePipe implements PipeTransform {
  transform(value: any): any {
      if (value !== undefined && value !== null) {
          return _.uniqBy(value, 'orgName');
      }
      return value;
  }
}
Run Code Online (Sandbox Code Playgroud)

ash*_*.gd 6

只需创建一个名为 as 的模块SharedModule,然后从中导出管道。这样,它就可以作为任何导入SharedModule.

例子:

import { CommonModule } from '@angular/common';
import { HttpClientModule } from '@angular/common/http';
import { NgModule } from '@angular/core';
import { UniquePipe} from './pipes/unique.pipe';




@NgModule({
  declarations: [
    UniquePipe,
  ],
  imports: [
    CommonModule,
    HttpClientModule,
  ],
  exports: [
    UniquePipe,
  ]
})
export class SharedModule {}

Run Code Online (Sandbox Code Playgroud)

您可以在以下位置阅读更多相关信息:

  1. https://angular.io/guide/sharing-ngmodules#sharing-modules
  2. 风格指南:https : //angular.io/guide/styleguide#shared-feature-module