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

标签 javascript angular angular-pipe

我正在尝试制作一个共享模块,但不知何故它不想工作。

共享模块如下所示:

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 };
    }
}

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

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

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

如果我不在 profile-gallery 模块中导入共享模块,我会得到这个错误:

找不到管道“键”。

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

MetaModule 已经加载;仅在根模块中导入。

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

最佳答案

共享模块的目的是在所有需要的模块中多次导入它。所以不需要 forRoot 方法来确保它只被导入一次。

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

import { SharedModule } from '@shared/shared.module';
@NgModule({
    imports: [
        ...
        SharedModule,

关于javascript - Angular 6 - 为子模块导入共享模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50203325/

相关文章:

php - 使可编辑的 div 在 div 长度末尾换行

javascript - ng hide,如何有条件地显示带有 url 扩展名的 Angular 内容

css - ngx-material-timepicker : iPhone css ignores "position: fixed"

angular - 如何将数字转换为 Angular 2组件:内的字符串

json - Angular Json 管道类型错误 : Converting circular structure to JSON

javascript - 这个模式 f(a)(b)(c) 叫什么?

javascript - 在从 CFC 返回结果之前对 AJAX 查询执行字符串函数

Angular queryParamMap 为空,然后填充

Angular 4 - 如何在输入类型中使用货币管道

Angular 十进制管道不显示前导零?