Angular2 Pipes RC6. Can I declare a feed in the same .ts file as my component?

Do I need to register each channel with @ngModule? I have one that is used by only one specific component. It would be nice to declare this in the same file, so nothing else knows about it. I believe that this was possible earlier - I just can’t find the correct syntax.

Thank.

+4
source share
2 answers

Each pipe, component and directive must be declared in the module. This is because the compiler relies on module declarations for template analysis. If he did not see MyPipein the module declarations (or declarations of the imported module), then he will not look for it and not recognize when you use the channel in the template

If it is important that no other component can see your pipe, create a module that contains only the component that uses the pipe and the pipe itself, and do not export this pipe.

import { NgModule }      from '@angular/core';
import { CommonModule }  from '@angular/common';
import { MyPipe }        from './my.pipe';
import { MyComponent }   from './my.component';

@NgModule({
  imports:      [ CommonModule ],
  declarations: [ MyComponent, MyPipe ],
  exports:      [], // don't export what you want to keep private
  providers:    []
})
export class MyModule { }
+7
source

Since RC6, the property has pipesbeen removed from ComponentTypeMetadata, you need to register them with NgModule.

+1
source

Source: https://habr.com/ru/post/1653613/


All Articles