How to declare a pipe globally to use in different modules?

后端 未结 5 1895
鱼传尺愫
鱼传尺愫 2020-12-02 15:29

I have a custom pipe named CurrConvertPipe

import {Pipe, PipeTransform} from \'@angular/core\';
import {LocalStorageService} f         


        
5条回答
  •  暖寄归人
    2020-12-02 15:38

    you can use Sharing Modules for share your service, directive, pipes, components

    you have to create an module and import the pipes ,directive, services or components and set the declaration, export and providers for the services.

    import the sharing module in to where ever you want and use it.

    basically pipes and directives declared and exported in NgModules meta data. for services define forRoot which returns the providers to access other modules.

    • shareModule.ts

      
      import { NgModule, ModuleWithProviders } from '@angular/core';
      import { appDirective } from './{your-path}';
      import { appPipe } from './{your-path}';
      import { appService } from './{your-path}';
      
      @NgModule({
        declarations: [
          appPipe,
          appDirective
        ],
        exports: [
          appPipe,
          appDirective
        ]
      })
      export class SharingModule {
        static forRoot(): ModuleWithProviders {
          return {
            ngModule: SharingModule,
            providers: [ appService ]
          };
        }
      }
      
    • my-module1.module.ts

      
      import { BrowserModule } from '@angular/platform-browser';
      import { NgModule } from '@angular/core';
      
      import { myComponent } from './{your-path}';
      
      import { SharingModule } from './{your-path}';
      
      @NgModule({
        declarations: [
          myComponent
        ],
        imports: [
          BrowserModule,
          SharingModule.forRoot()  
        ],
      })
      export class AppModule {}
      

    Like wise you can do in othe moduls also.

提交回复
热议问题