Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/422.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何将角度管道用作共享组件或共享模块_Javascript_Angular_Angular6 - Fatal编程技术网

Javascript 如何将角度管道用作共享组件或共享模块

Javascript 如何将角度管道用作共享组件或共享模块,javascript,angular,angular6,Javascript,Angular,Angular6,我使用下面的函数作为管道,用于获取下拉列表中的唯一值。我需要在多个组件中使用它。如何创建可重用组件以使用此功能 @Pipe({ name: 'unique', pure: false }) export class UniquePipe implements PipeTransform { transform(value: any): any { if (value !== undefined && value !== null) {

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

 @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;
  }
}
在shared.module.ts文件中,您只需将其添加到声明和导出中,然后就可以将此模块导入要使用此管道的任何其他模块中

in-pipe.components.ts

然后在shared.module.ts中:

在shared.module.ts文件中,您只需将其添加到声明和导出中,然后就可以将此模块导入要使用此管道的任何其他模块中

in-pipe.components.ts

然后在shared.module.ts中:

只需创建一个名为SharedModule的模块,然后导出 管道从它。这样,它就可以作为任何应用程序的公共API提供 导入SharedModule的模块的

例如:

从“@angular/common”导入{CommonModule}; 从'@angular/common/http'导入{HttpClientModule}; 从“@angular/core”导入{NgModule}; 从“./pipes/unique.pipe”导入{UniquePipe}; @NGD模块{ 声明:[ 独特的管道, ], 进口:[ 公共模块, HttpClientModule, ], 出口:[ 独特的管道, ] } 导出类SharedModule{} 您可以通过以下网址了解更多信息:

样式指南: 只需创建一个名为SharedModule的模块,然后导出 管道从它。这样,它就可以作为任何应用程序的公共API提供 导入SharedModule的模块的

例如:

从“@angular/common”导入{CommonModule}; 从'@angular/common/http'导入{HttpClientModule}; 从“@angular/core”导入{NgModule}; 从“./pipes/unique.pipe”导入{UniquePipe}; @NGD模块{ 声明:[ 独特的管道, ], 进口:[ 公共模块, HttpClientModule, ], 出口:[ 独特的管道, ] } 导出类SharedModule{} 您可以通过以下网址了解更多信息:

样式指南:
“共享”是什么意思?你是说在多组件HTML上使用它吗?是的,我需要在多组件中使用它,你不需要做任何额外的事情。只需将其导入相应的模块,就可以在组件上使用它。这不是管道的设计目的。它们应用于在打印以查看时格式化数据,而不是用于任意数据操作。应用程序逻辑属于组件代码,而不是模板。你所说的“共享”是什么意思?你是指在多组件HTML上使用它吗?是的,我需要在多组件中使用它。你不需要做任何额外的事情。只需将其导入相应的模块,就可以在组件上使用它。这不是管道的设计目的。它们应用于在打印以查看时格式化数据,而不是用于任意数据操作。应用程序逻辑属于组件代码,而不是模板。
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
  name: 'mypipe',
})
export class MyPipe implements PipeTransform {
  // convert dictionary to list so that it can be iterated in html
  transform(objects: any = []) {
    return Object.values(objects);
  }
}
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { ValueArrayPipe } from '../pipe/pipe.component';


@NgModule({
    imports: [
        CommonModule
    ],
    declarations: [ValueArrayPipe ],
exports: [ValueArrayPipe ],
})
export class SharedPipesModule { }