简体   繁体   English

Angular2:找不到自定义管道

[英]Angular2: custom pipe could not be found

The built-in pipe is work,but all custom pipes that i wanna use are the same error:内置管道可以工作,但我想使用的所有自定义管道都是相同的错误:

the pipe 'actStatusPipe' could not be found找不到管道“actStatusPipe”

[ERROR ->]{{data.actStatus | [错误->]{{data.actStatus | actStatusPipe}}行为状态管道}}

I have tried two ways,declare it in app.module's declarations:我尝试了两种方法,在 app.module 的声明中声明:

app.module.ts: app.module.ts:

import {ActStatusPipe} from '../pipe/actPipe'

@NgModule({
    declarations: [
        AppComponent,
        HomePage,
        ActivitiesList,
        ActStatusPipe
    ],
    ...
})

or use other module to declare and export all my pipes: //pipe或使用其他模块声明和导出我所有的管道://管道

import {ActStatusPipe} from "./actPipe"

@NgModule({
    declarations:[ActStatusPipe],
    imports:[CommonModule],
    exports:[ActStatusPipe]
})

export class MainPipe{}

and import it in app.module.并将其导入 app.module。

//pipe
import {MainPipe} from '../pipe/pipe.module'
    
@NgModule({
    declarations:[...],
    imports:[...,MainPipe],
})

But none of them work in my app.但它们都不适用于我的应用程序。

Here is my code of the pipe:这是我的管道代码:

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

@Pipe({
    name:'actStatusPipe'
})
export class ActStatusPipe implements PipeTransform{
    transform(status:any):any{
        switch (status) {
            case 1:
                return "UN_PUBLISH";
            case 2:
                return "PUBLISH";
            default:
                return status
        }
    }
}

I think it is most of the same with the document(In fact,i have just copied from the document and made a little modification)我认为它与文档大部分相同(实际上,我只是从文档中复制并进行了一些修改)

And my angular2's version is 2.1.而我的 angular2 的版本是 2.1。

Lots of solution that can be searched in stackOverflow and google are tried in my app,however they don't work.在我的应用程序中尝试了很多可以在 stackOverflow 和 google 中搜索的解决方案,但是它们不起作用。

This confused me a lot,thanks for you answer!这让我很困惑,谢谢你的回答!

see this is working for me.看到这对我有用。

ActStatus.pipe.ts First this is my pipe ActStatus.pipe.ts首先这是我的管道

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

@Pipe({
  name:'actStatusPipe'
})
export class ActStatusPipe implements PipeTransform{
  transform(status:any):any{
    switch (status) {
      case 1:
        return "UN_PUBLISH";
      case 2:
        return "PUBLISH";
      default:
        return status
    }
  }
}

main-pipe.module.ts in pipe module, i need to declare my pipe/s and export it.管道模块中的main-pipe.module.ts ,我需要声明我的管道并导出它。

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

import {ActStatusPipe} from "./ActStatusPipe.pipe"; // <---

@NgModule({
  declarations:[ActStatusPipe], // <---
  imports:[CommonModule],
  exports:[ActStatusPipe] // <---
})

export class MainPipe{}

app.module.ts user this pipe module in any module. app.module.ts在任何模块中使用这个管道模块。

@NgModule({
  declarations: [...],
  imports: [..., MainPipe], // <---
  providers: [...],
  bootstrap: [AppComponent]
})

you can directly user pipe in this module.您可以直接在此模块中使用管道。 but if you feel that your pipe is used with in more than one component i suggest you to follow my approach.但是如果您觉得您的管道用于多个组件,我建议您遵循我的方法。

  1. create pipe .创建管道。
  2. create separate module and declare and export one or more pipe.创建单独的模块并声明和导出一个或多个管道。
  3. user that pipe module.用户该管道模块。

How to use pipe totally depends on your project complexity and requirement.如何使用管道完全取决于您的项目复杂性和要求。 you might have just one pipe which used only once in the whole project.您可能只有一根在整个项目中仅使用一次的管道。 in that case you can directly use it without creating a pipe/s module (module approach).在这种情况下,您可以直接使用它而无需创建 pipe/s 模块(模块方法)。

This didnt worked for me.这对我不起作用。 (Im with Angular 2.1.2). (我使用 Angular 2.1.2)。 I had NOT to import MainPipeModule in app.module.ts and importe it instead in the module where the component Im using the pipe is imported too.我不必在 app.module.ts 中导入 MainPipeModule 而是将它导入到使用管道的组件 Im 也导入的模块中。

Looks like if your component is declared and imported in a different module, you need to include your PipeModule in that module too.看起来如果您的组件是在不同的模块中声明和导入的,您也需要在该模块中包含您的 PipeModule。

A really dumb answer (I'll vote myself down in a minute), but this worked for me:一个非常愚蠢的答案(我会在一分钟内给自己投票),但这对我有用:

After adding your pipe, if you're still getting the errors and are running your Angular site using " ng serve ", stop it... then start it up again.添加管道后,如果您仍然遇到错误并且正在使用“ ng serve ”运行您的Angular站点,请停止它......然后再次启动它。

For me, none of the other suggestions worked, but simply stopping, then restarting " ng serve " was enough to make the error go away.对我来说,其他建议都不起作用,只是停止,然后重新启动“ ng serve ”就足以使错误消失。

Strange.奇怪的。

I take no issue with the accepted answer as it certainly helped me.我对接受的答案没有异议,因为它肯定对我有帮助。 However, after implementing it, I still got the same error.但是,实施后,我仍然遇到相同的错误。

Turns out this was because I was calling the pipes incorrectly in my component as well:原来这是因为我在我的组件中也错误地调用了管道:

My custom-pipe.ts file:我的 custom-pipe.ts 文件:

@Pipe({ name: 'doSomething' })
export class doSomethingPipe implements PipeTransform {
    transform(input: string): string {
        // Do something
    }
}

So far, so good, but in my component.html file I was calling the pipes as follows:到目前为止,一切都很好,但是在我的 component.html 文件中,我按如下方式调用管道:

{{ myData | doSomethingPipe }}

This will again throw the error that the pipe is not found.这将再次抛出未找到管道的错误。 This is because Angular looks up the pipes by the name defined in the Pipe decorator.这是因为 Angular 通过管道装饰器中定义的名称查找管道。 So in my example, the pipe should instead be called like this:所以在我的例子中,应该像这样调用管道:

{{ myData | doSomething }}

Silly mistake, but it cost me a fair amount of time.愚蠢的错误,但它花费了我相当多的时间。 Hope this helps!希望这可以帮助!

import {CommonModule} from "@angular/common";

将此语句添加到管道模块解决了我的问题。

be sure, that if the declarations for the pipe are done in one module, while you are using the pipe inside another module, you should provide correct imports/declarations at the current module under which is the class where you are using the pipe.可以肯定的是,如果管道的声明是在一个模块中完成的,当您在另一个模块中使用管道时,您应该在当前模块中提供正确的导入/声明,该模块是您使用管道的类。 In my case that was the reason for the pipe miss在我的情况下,这是管道未命中的原因

Also make sure your pipe name (inside the decorator) is v̲i̲s̲i̲b̲l̲y camelCased.还要确保您的管道名称(在装饰器内)是 v̲i̲s̲i̲b̲l̲y camelCased。

This does not work : @Pipe({ name: 'plural' }) ❌,不起作用@Pipe({ name: 'plural' }) ❌,
but this does : @Pipe({ name: 'pluralForm' })但这确实是@Pipe({ name: 'pluralForm' })

如果您要在另一个模块中声明管道,请确保将其添加到该模块的声明和导出数组中,然后将该模块导入到使用该管道的任何模块中。

I encountered a similar issue, but putting it in my page's module didn't work.我遇到了类似的问题,但将其放入我的页面模块不起作用。

I had created a component, which needed a pipe.我创建了一个需要管道的组件。 This component was declared and exported in a ComponentsModule file, which holds all of the app's custom components.该组件是在 ComponentsModule 文件中声明和导出的,该文件包含应用程序的所有自定义组件。

I had to put my PipesModule in my ComponentsModule as an import, in order for these components to use these pipes and not in the page's module using that component.我必须将我的 PipesModule 作为导入放在我的 ComponentsModule 中,以便这些组件使用这些管道而不是使用该组件的页面模块。

Credits: enter link description here Answer by: tumain致谢: 在此处输入链接描述答案:tumain

I faced similar issue.Mine got resolved by :-我遇到了类似的问题。我的问题解决了:-

  1. Importing the pipe's module(SharedModule) into the required module.将管道的模块(SharedModule)导入所需的模块。
  2. Add the custom pipe in the providers array.在提供者数组中添加自定义管道。 @NgModule({ imports: [ SharedModule ], providers: [CustomPipe] )}

Please make sure you're using the 'selector' while using th pipe.请确保在使用管道时使用“选择器”。 for eg:例如:

import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
  name: 'removeApos'
})
export class RemoveAposPipe implements PipeTransform {
  transform(value: unknown, ...args: unknown[]): unknown {
    return null;
  }
}

you need to use this as你需要用这个作为

<div>{{someval | removeApos}}</div>

please note the selector in the above eg and its usage in the div请注意上面示例中的选择器及其在 div 中的用法

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

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