Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 从共享文件夹导入角度自定义管道时出错_Angular_Typescript_Module_Single Page Application_Angular9 - Fatal编程技术网

Angular 从共享文件夹导入角度自定义管道时出错

Angular 从共享文件夹导入角度自定义管道时出错,angular,typescript,module,single-page-application,angular9,Angular,Typescript,Module,Single Page Application,Angular9,我从共享文件夹中创建了一个自定义管道,以便在组件1(例如:申请者组件)HTML中使用它,因此我将其导入到applicationModule。我的目标是使此管道在组件2中可重用(例如:申请者组件),因此我还将此自定义管道导入组件的2模块,即coapplicationModule。编译后,我得到了控制台错误 当我尝试将自定义管道的import语句移动到SharedModule并尝试从组件2导入管道时,我得到了一个控制台错误。 我想寻求您的帮助,因为找不到此模块的错误,因为我假设它会工作,因为Sh

我从共享文件夹中创建了一个自定义管道,以便在组件1(例如:申请者组件)HTML中使用它,因此我将其导入到applicationModule。我的目标是使此管道在组件2中可重用(例如:申请者组件),因此我还将此自定义管道导入组件的2模块,即coapplicationModule。编译后,我得到了控制台错误

当我尝试将自定义管道的import语句移动到SharedModule并尝试从组件2导入管道时,我得到了一个控制台错误。


我想寻求您的帮助,因为找不到此模块的错误,因为我假设它会工作,因为SharedModule也导入了AppliantModuleCoAppliantModule

您的错误表明您在两个模块中声明了
管道。您在错误消息中找到了解决方案。您可以创建另一个模块,将
管道添加到
声明中,然后在
导出中导出,而不是“导入”您的
管道
通过
声明
您只需在
导入
中导入创建的模块。您的错误表明您已在两个模块中声明了
管道
。您在错误消息中找到了解决方案。您可以创建另一个模块,将
管道添加到
声明中,然后在
导出中导出,而不是“导入”您的
管道
通过
声明
您只需在
导入
中导入创建的模块

您不能在多个
模块
中声明您的
管道
。您可以为所有自定义管道创建一个
模块
,然后将其导入到需要使用它们的所有组件中


我通常创建一个目录,以使事情井然有序。在本例中,管道目录

管道/管道模块.ts

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { MyCustomPipe } from './myCustom.pipe';

@NgModule({
  declarations: [MyCustomPipe],
  imports: [
    CommonModule
  ],
  entryComponents: [MyCustomPipe],
  exports: [MyCustomPipe]
})
export class PipesModule { }
...
import { PipesModule } from "../../pipes/pipes.module";
...

@NgModule({
  imports: [
    ...,
    PipesModule
  ],
  declarations: [HomePage]
})
export class HomePageModule {}

现在,您可以在任何组件中重用它。例如:

页面/home/home.module.ts

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { MyCustomPipe } from './myCustom.pipe';

@NgModule({
  declarations: [MyCustomPipe],
  imports: [
    CommonModule
  ],
  entryComponents: [MyCustomPipe],
  exports: [MyCustomPipe]
})
export class PipesModule { }
...
import { PipesModule } from "../../pipes/pipes.module";
...

@NgModule({
  imports: [
    ...,
    PipesModule
  ],
  declarations: [HomePage]
})
export class HomePageModule {}
现在,它们已经在这里提供了

pages/home/home.page.html

...
<p>{{ something | myCustomPipe }}</p>
...
。。。
{{something | myCustomPipe}}

...
您不能在多个
模块
中声明您的
管道
。您可以为所有自定义管道创建一个
模块
,然后将其导入到需要使用它们的所有组件中


我通常创建一个目录,以使事情井然有序。在本例中,管道目录

管道/管道模块.ts

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { MyCustomPipe } from './myCustom.pipe';

@NgModule({
  declarations: [MyCustomPipe],
  imports: [
    CommonModule
  ],
  entryComponents: [MyCustomPipe],
  exports: [MyCustomPipe]
})
export class PipesModule { }
...
import { PipesModule } from "../../pipes/pipes.module";
...

@NgModule({
  imports: [
    ...,
    PipesModule
  ],
  declarations: [HomePage]
})
export class HomePageModule {}

现在,您可以在任何组件中重用它。例如:

页面/home/home.module.ts

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { MyCustomPipe } from './myCustom.pipe';

@NgModule({
  declarations: [MyCustomPipe],
  imports: [
    CommonModule
  ],
  entryComponents: [MyCustomPipe],
  exports: [MyCustomPipe]
})
export class PipesModule { }
...
import { PipesModule } from "../../pipes/pipes.module";
...

@NgModule({
  imports: [
    ...,
    PipesModule
  ],
  declarations: [HomePage]
})
export class HomePageModule {}
现在,它们已经在这里提供了

pages/home/home.page.html

...
<p>{{ something | myCustomPipe }}</p>
...
。。。
{{something | myCustomPipe}}

...