无法找到自定义管道

问题描述 投票:1回答:1

我已经建了一个管子,但我不知道为什么棱角分明找不到它。

我的顶级模块是我的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(
    ) {
    }

}
angular pipe angular-pipe
1个回答
1
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.