Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2 - What are declarables?

Tags:

angular

I understand a declarable:

  • is either a component, directive or pipe
  • can be added to a module's declarations array
  • belongs to one, and only one, module

But what actually is a declarable? A service class sort of belongs to a module, so why is a service class not a declarable? Or, more generally, what does it mean to declare something inside a module?

like image 280
dayuloli Avatar asked Sep 06 '25 10:09

dayuloli


2 Answers

Declarables allow the angular compiler to know which module will actually contain the component, directive or pipe.

As the compiler generates the factories that make the views, it will integrate those components with the module they were declared, and only refer to them in any other module that might be using them.

This is why you only want each component, pipe and directive to only be declared in a maximum of one module.

Services are put together with the rest and referenced as needed by other tools such as webpack or in some cases indirectly by the browser. The angular compiler does not need to compile the service code, it just uses them as normal javascript code.

Edit: Specify that services don't need to be compiled by the angular compiler. Of course if it's typescript it will still be compiled but by the typescript compiler not the angular one.

like image 170
Ben Dadsetan Avatar answered Sep 09 '25 07:09

Ben Dadsetan


declarations makes selectors and pipe names known to Angular.
When it compiles a components template, it looks up matching components, directives and pipes in declarations of the current module and imported modules, for them to be applied to the elements where selectors and pipe names match.

like image 31
Günter Zöchbauer Avatar answered Sep 09 '25 05:09

Günter Zöchbauer