无法找到自定义管道

时间:2018-03-01 09:23:53

标签: angular pipe angular-pipe

我已经制作了一个管道,但是我无法说明为什么角度无法找到它。

我的顶级模块是我的shared.module

该模块又由browser.module或server.module集成。

此外,我有一个user.module作为最低级别,所有用户的组件都进入。

如果我在user.module中定义管道,则会找到管道。

import { ExponentialStrengthPipe } from '../../exponential-strength.pipe';
[...]

@NgModule({
    declarations: [ [...]

        ExponentialStrengthPipe
    ],
    imports: [
        CommonModule,
        HttpModule,
        FormsModule,
        ReactiveFormsModule,
        RouterModule.forChild([...])
    ],

})
export class UserModule {
    constructor(
    ) {
    }

}

如果我在共享中安装它。模块,没有了。

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { ExponentialStrengthPipe } from './exponential-strength.pipe';
[...]
@NgModule({
    declarations: [
        AppComponent,
[...]
        ExponentialStrengthPipe
    ],

    providers: [
        UserService

    ],
    imports: [
        CommonModule,
        RouterModule.forRoot([
            { path: 'home', component: HomeComponent },
            { path: '', redirectTo: 'home', pathMatch: 'full' },
            { path: '**', component: PageNotFoundComponent },
        ]),
    ],
    exports: [
        ExponentialStrengthPipe
        ]

})
export class AppModuleShared {
    constructor(
    ) {
    }

}

2 个答案:

答案 0 :(得分:1)

如果要在其他模块中使用管道,请添加声明管道的模块  imports: [...]  要重新使用管道的模块,而不是将其添加到模块的declarations: []。 如果您在多个declarations:[]中添加管道,则会收到错误,抱怨您已在多个模块中声明了管道。 因此,在这种情况下,您必须在共享模块的declarations: []中添加管道,并将共享模块添加到要使用管道的模块中的imports: [...]。 (例如:用户模块)

答案 1 :(得分:0)

您必须将管道包含在AppModule的声明数组中 如果选择将管道注入到类中,则必须在NgModule的providers数组中提供它。

相关问题