Angular 6 - 为子模块导入共享模块

use*_*731 8 javascript angular-pipe angular

我正在尝试创建一个共享模块,但它不想以某种方式工作.

共享模块如下所示:

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

import { SharedMetaModule } from './shared-meta';
import { ApplicationState } from './state/application-state';
import { MilitaryTimePipe } from './pipes/military-time-pipe';
import { KeysPipe } from './pipes/object-pipe';
import { GirlsClass } from './advertisers/girls';

@NgModule({
    imports: [CommonModule],
    declarations: [
        KeysPipe,
        MilitaryTimePipe
    ],
    exports: [
        SharedMetaModule,
        KeysPipe,
        MilitaryTimePipe
    ],
    providers: [ApplicationState]
})
export class SharedModule {
    static forRoot(): ModuleWithProviders {
        return { ngModule: SharedModule };
    }
}
Run Code Online (Sandbox Code Playgroud)

我有一个像这样的app.module.ts:

import { SharedModule } from '@shared/shared.module';
@NgModule({
    imports: [
        ...
        SharedModule.forRoot(),
Run Code Online (Sandbox Code Playgroud)

然后我有一个profile-gallery.module.ts,其中将使用来自共享模块的管道.

如果我不在配置文件库模块中导入共享模块,我收到此错误:

The pipe 'keys' could not be found.

如果我将共享模块导入profile-gallery模块,我收到此错误:

MetaModule already loaded; import in root module only.

在这种情况下如何使用共享模块?

Adr*_*ciu 6

共享模块的目的是将其导入所有必需的模块,不止一次.因此,不需要forRoot方法来确保它只导入一次.

完全删除forRoot方法并在需要的地方导入模块:

import { SharedModule } from '@shared/shared.module';
@NgModule({
    imports: [
        ...
        SharedModule,
Run Code Online (Sandbox Code Playgroud)