【发布时间】:2021-12-01 08:31:48
【问题描述】:
我的管道位于apps\administrator\src\app\modules\messenger\pipes\custom-message.pipe.ts
import { Pipe, PipeTransform } from '@angular/core';
/**
* Pipe for Custom Message for boolean
*/
@Pipe({
name: 'customMessage',
})
export class CustomMessagePipe implements PipeTransform {
public transform(value: boolean, trueString: string, falseString: string): string {
//The code
}
}
在主模块apps\administrator\src\app\app.module.ts文件中:
import { CustomMessagePipe } from './modules/messenger/pipes/custom-message.pipe';
...
@NgModule({
declarations: [..., CustomMessagePipe],
providers: [...],
})
export class AppModule {}
现在,我有两个模块 FormSmsModule 和 FormSmtpModule
FormSmsModule 位于apps\administrator\src\app\modules\messenger\form-sms
FormSmtpModule 位于apps\administrator\src\app\modules\messenger\form-smtp
按照这个答案https://stackoverflow.com/a/62468805
在文件 apps\administrator\src\app\modules\messenger\form-sms\form-sms.module.ts 中使用CustomMessagePipe imports 数组,我有:
import { CustomMessagePipe } from '../pipes/custom-message.pipe';
@NgModule({
declarations: [...],
imports: [
...,
CustomMessagePipe,
FormSmsRoutingModule,
],
providers: [...],
})
export class FormSmsModule {}
在文件 apps\administrator\src\app\modules\messenger\form-smtp\ form-smtp.module.ts 中使用CustomMessagePipe imports 数组,我有:
import { CustomMessagePipe } from '../pipes/custom-message.pipe';
@NgModule({
declarations: [...],
imports: [
...,
CustomMessagePipe,
FormSmtpRoutingModule,
],
providers: [...],
})
export class FormSmtpModule {}
在这种形式中,我有类似error NG6002: Appears in the NgModule.imports of AppModule, but could not be resolved to an NgModule class的错误
ERROR in apps/administrator/src/app/modules/messenger/pipes/custom-message.pipe.ts:10:14 - error NG6002: Appears in the NgModule.imports of FormSmsModule, but could not be resolved to an NgModule class.
Is it missing an @NgModule annotation?
10 export class CustomMessagePipe implements PipeTransform {
~~~~~~~~~~~~~~~~~
apps/administrator/src/app/modules/messenger/pipes/custom-message.pipe.ts:10:14 - error NG6002: Appears in the NgModule.imports of FormSmtpModule, but could not be resolved to an NgModule class.
Is it missing an @NgModule annotation?
10 export class CustomMessagePipe implements PipeTransform {
~~~~~~~~~~~~~~~~~
** Angular Live Development Server is listening on localhost:4202, open your browser on http://localhost:4202/ **
使用替代方法作为https://stackoverflow.com/a/40015085
在文件 apps\administrator\src\app\modules\messenger\form-sms\form-sms.module.ts 中使用CustomMessagePipe declarations 数组,我有:
import { CustomMessagePipe } from '../pipes/custom-message.pipe';
@NgModule({
declarations: [..., CustomMessagePipe],
imports: [
...,
FormSmsRoutingModule,
],
providers: [...],
})
export class FormSmsModule {}
在文件 apps\administrator\src\app\modules\messenger\form-smtp\form-smtp.module.ts 中使用CustomMessagePipe declarations 数组,我有:
import { CustomMessagePipe } from '../pipes/custom-message.pipe';
@NgModule({
declarations: [..., CustomMessagePipe],
imports: [
...,
FormSmtpRoutingModule,
],
providers: [...],
})
export class FormSmtpModule {}
我有这个问题中描述的错误错误Angular 2 - Pipe reuse in multiple modules - error not found or duplicate definition
ERROR in apps/administrator/src/app/modules/messenger/pipes/custom-message.pipe.ts:10:14 - error NG6007: The Pipe 'CustomMessagePipe' is declared by more than one NgModule.
10 export class CustomMessagePipe implements PipeTransform {
~~~~~~~~~~~~~~~~~
apps/administrator/src/app/modules/messenger/form-sms/form-sms.module.ts:47:84
47 declarations: [..., CustomMessagePipe],
~~~~~~~~~~~~~~~~~
'CustomMessagePipe' is listed in the declarations of the NgModule 'FormSmsModule'.
apps/administrator/src/app/modules/messenger/form-smtp/form-smtp.module.ts:47:85
47 declarations: [..., CustomMessagePipe],
~~~~~~~~~~~~~~~~~
'CustomMessagePipe' is listed in the declarations of the NgModule 'FormSmtpModule'.
apps/administrator/src/app/app.module.ts:36:112
36 declarations: [..., CustomMessagePipe],
~~~~~~~~~~~~~~~~~
'CustomMessagePipe' is listed in the declarations of the NgModule 'AppModule'.
** Angular Live Development Server is listening on localhost:4202, open your browser on http://localhost:4202/ **
如您所见,这两个解决方案意味着另一个问题。
如何解决?
【问题讨论】:
标签: angular typescript module angular-pipe