简体   繁体   English

没有找到名为“...”的 Pipe - Angular(2+)

[英]No Pipe found with name '...' - Angular(2+)

I made a custom pipe and want to use it in my project, but getting this error while implementing that pipe: "src/app/portfolio/port-main/port-main.component.html:11:124 - error NG8004: No pipe found with name 'filter'." I made a custom pipe and want to use it in my project, but getting this error while implementing that pipe: "src/app/portfolio/port-main/port-main.component.html:11:124 - error NG8004: No找到名为“过滤器”的 pipe。”

My filter.pipe.ts code:我的 filter.pipe.ts 代码:

   import { Pipe, PipeTransform } from '@angular/core';

   @Pipe({
     name: 'filter'
   })

Here is the code snippet where i wanna use this pipe: Want to mention that this component is inside a routing module which has lazy loading implemented.这是我想使用此 pipe 的代码片段:想提一下,该组件位于实现延迟加载的路由模块内。 That's why I didn't import this component into app.module.ts file.这就是我没有将此组件导入 app.module.ts 文件的原因。

 <div class="card mb-3 me-3 col-lg-6 col-md-12 col-sm-12" *ngFor="let img of PortfolioArray | filter:ValueText:'category'">
...
</div>

Here is my app.module.ts file where I import the custom pipe:这是我的 app.module.ts 文件,我在其中导入了自定义 pipe:

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { HomeComponent } from './home/home.component';
import { NavbarComponent } from './navbar/navbar.component';
import { NotFoundComponent } from './not-found/not-found.component';
import { SidebarComponent } from './sidebar/sidebar.component';
import { SidebarDownComponent } from './sidebar/sidebar-down/sidebar-down.component';
import { FilterPipe } from './pipes/filter.pipe';


@NgModule({
  declarations: [
    AppComponent,
    HomeComponent,
    NavbarComponent,
    NotFoundComponent,
    SidebarComponent,
    SidebarDownComponent,
    FilterPipe
  ],
  imports: [
    BrowserModule,
    AppRoutingModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

If anyone knows how to resolve this issue please let me know.如果有人知道如何解决此问题,请告诉我。 Willing to share more code if needed.如果需要,愿意分享更多代码。

Edited Here is the module where I'm using the pipe:编辑这里是我使用 pipe 的模块:

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';

import { PortfolioRoutingModule } from './portfolio-routing.module';
import { PortMainComponent } from './port-main/port-main.component';
import { FilterPipe } from '../pipes/filter.pipe';



@NgModule({
  declarations: [
    PortMainComponent
  ],
  imports: [
    CommonModule,
    PortfolioRoutingModule
  ],
  exports: [FilterPipe]
})
export class PortfolioModule { }

Based on your posted code, you could fix your issue by adding the FilterPipe to the declarations array in your PortfolioModule and removing it in AppModule .根据您发布的代码,您可以通过将FilterPipe添加到PortfolioModule中的declarations数组并在AppModule其删除来解决您的问题。

The issue you're currently facing is that you want to use a pipe/component/directive in a module where they're not declared.您当前面临的问题是您想在未声明它们的模块中使用管道/组件/指令。 As soon as you want to use one of those in multiple modules you need to create another module with which you can share these, let's call it SharedModule .只要您想在多个模块中使用其中一个,您就需要创建另一个可以共享这些模块的模块,我们称之为SharedModule

A shared module in your case looks like this:您的情况下的共享模块如下所示:

// An helper array to reduce code
const ELEMS = [FilterPipe];

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

The exports array is important because you can only use components/pipes/directives which are in that array in other modules. exports数组很重要,因为您只能在其他模块中使用该数组中的组件/管道/指令。

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

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