如何全局声明管道以在不同模块中使用? [英] How to declare a pipe globally to use in different modules?
问题描述
我有一个名为 CurrConvertPipe
import {Pipe, PipeTransform} from '@angular/core';
import {LocalStorageService} from './local-storage';
@Pipe({name: 'currConvert', pure: false})
export class CurrConvertPipe implements PipeTransform {
constructor(private currencyStorage: LocalStorageService) {}
transform(value: number): number {
let inputRate = this.currencyStorage.getCurrencyRate('EUR');
let outputputRate = this.currencyStorage.getCurrencyRate(localStorage.getItem('currency'));
return value / inputRate * outputputRate;
}
}
我需要在两个不同的模块Module1
和Module2
中使用它.
导入Module1
和Module2
时,出现错误消息,指出应在更高级别的模块中声明它.
所以我在 app.module.ts
import './rxjs-extensions';
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { CurrConvertPipe } from './services/currency/currency-pipe';
@NgModule({
imports: [
BrowserModule,
FormsModule,
HttpModule,
AppRoutingModule,
Module1,
Module2
],
declarations: [
AppComponent,
CurrConvertPipe
],
providers: [
],
bootstrap: [AppComponent]
})
export class AppModule { }
但是当我在Module1
中使用它时,会引发错误
找不到管道'currConvert'
在Angular中,共享常见指令,组件,管道等的一种好方法是使用所谓的共享模块. /p>
这些模块声明并导出公用部分,以使它们可用于您的任何其他模块.
角度文档的 基础知识部分 非常好了解共享模块.
以您的currConvert
管道为例.
-
声明一个名为
ApplicationPipesModule
的新NgModule
-
将管道添加到
NgModule
装饰器元数据的declarations
和exports
数组 -
将管道工作所需的所有模块添加到
imports
数组// application-pipes.module.ts // other imports import { CurrConvertPipe } from './{your-path}'; @NgModule({ imports: [ // dep modules ], declarations: [ CurrConvertPipe ], exports: [ CurrConvertPipe ] }) export class ApplicationPipesModule {}
-
通过将创建的
ApplicationPipesModule
模块添加到imports
数组中,将其导入到要使用管道的模块中// my-module1.module.ts // other imports import { ApplicationPipesModule } from './{your-path}'; @NgModule({ imports: [ // other dep modules ApplicationPipesModule ], declarations: [], exports: [] }) export class MyModule1 {}
I have a custom pipe named CurrConvertPipe
import {Pipe, PipeTransform} from '@angular/core';
import {LocalStorageService} from './local-storage';
@Pipe({name: 'currConvert', pure: false})
export class CurrConvertPipe implements PipeTransform {
constructor(private currencyStorage: LocalStorageService) {}
transform(value: number): number {
let inputRate = this.currencyStorage.getCurrencyRate('EUR');
let outputputRate = this.currencyStorage.getCurrencyRate(localStorage.getItem('currency'));
return value / inputRate * outputputRate;
}
}
I need to use this in two different modules, Module1
and Module2
.
When I import in Module1
and Module2
, I get an error saying it should be declared in a higher level module.
So I declare the pipe inside the app.module.ts
import './rxjs-extensions';
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { CurrConvertPipe } from './services/currency/currency-pipe';
@NgModule({
imports: [
BrowserModule,
FormsModule,
HttpModule,
AppRoutingModule,
Module1,
Module2
],
declarations: [
AppComponent,
CurrConvertPipe
],
providers: [
],
bootstrap: [AppComponent]
})
export class AppModule { }
But when I use it in Module1
, it throws an error
The pipe 'currConvert' could not be found
In Angular a good technique for sharing common directives, components, pipes, etc. is to use a so called shared module.
Those modules declare and export common parts, to make them usable for any of your other modules.
The fundamentals section of the angular documentation is a very good read about shared modules.
Let's take as example your currConvert
pipe.
Declare new NgModule called
ApplicationPipesModule
Add the pipe to the
declarations
andexports
arrays of theNgModule
-decorator metadataAdd any modules that may be required for your pipe to work to the
imports
array// application-pipes.module.ts // other imports import { CurrConvertPipe } from './{your-path}'; @NgModule({ imports: [ // dep modules ], declarations: [ CurrConvertPipe ], exports: [ CurrConvertPipe ] }) export class ApplicationPipesModule {}
import the created
ApplicationPipesModule
module into the modules where your pipe is going to be used, by adding it to theimports
array// my-module1.module.ts // other imports import { ApplicationPipesModule } from './{your-path}'; @NgModule({ imports: [ // other dep modules ApplicationPipesModule ], declarations: [], exports: [] }) export class MyModule1 {}
这篇关于如何全局声明管道以在不同模块中使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!