Ionic 4 中 2 个模块的声明

Declarations of 2 modules in Ionic 4

我有 2 个页面,有 2 个模块,并尝试使用相同的管道,所以我在每个页面中都声明了相同的管道。

listview.module.ts

import { DateTimePipe } from '../pipes/dateTime.pipe';

declarations: [ListviewPage, DateTimePipe,]

profile.module.ts

import { DateTimePipe } from '../pipes/dateTime.pipe';

declarations: [ProfilePage, DateTimePipe]

为此,我遇到了这个问题:

ERROR Error: Uncaught (in promise): Error: Type DateTimePipe is part of the declarations of 2 modules: ListviewPageModule and ProfilePageModule! Please consider moving DateTimePipe to a higher module that imports ListviewPageModule and ProfilePageModule. You can also create a new NgModule that exports and includes DateTimePipe then import that NgModule in ListviewPageModule and ProfilePageModule. Error: Type DateTimePipe is part of the declarations of 2 modules: ListviewPageModule and ProfilePageModule! Please consider moving DateTimePipe to a higher module that imports ListviewPageModule and ProfilePageModule. You can also create a new NgModule that exports and includes DateTimePipe then import that NgModule in ListviewPageModule and ProfilePageModule.

我尝试在 app.module.ts 中声明它,但没有用。

谢谢

您需要制作一个共享模块,将您的管道放入该模块,然后根据需要将共享模块导入到其他地方。

SharedModule.ts

import { NgModule } from '@angular/core';
import { DateTimePipe } from '../pipes/dateTime.pipe';
import { CommonModule } from '@angular/common';  

@NgModule({
    imports: [
        CommonModule
    ],
    declarations: [
        DateTimePipe
    ],
    exports: [
        DateTimePipe
    ]
})
export class SharedModule {}

listview.module.ts

...
imports: [SharedModule]
...

profile.module.ts

...
imports: [SharedModule]
...
import { NgModule } from '@angular/core';
import { DateTimePipe } from '../pipes/dateTime.pipe';
import { CommonModule } from '@angular/common';  

@NgModule({
    imports: [
        IonicModule,
        CommonModule
    ],
    declarations: [
        DateTimePipe
    ],
    exports: [
        DateTimePipe
    ]
})
export class SharedModule {}

然后按照@Shannon 的回答。 ionic4 应用程序需要 IonicModule