Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sub-components in Angular 2

Tags:

angular

In Angular 2 I have a component that is clearly a sub-component to another component. For instance, a Contact component within a ContactsPage component. Since the introduction of modules, I must now add ContactComponent to the declaration-section of my NgModule, making the Contact component available in all HTML templates in the module. This gives me two problems:

  1. The component hierarchy tree is flattened into a list which makes the project structure harder to follow
  2. The namespace is also flattened, I'd rather get an error if i accidentally reference <contact> outside ContactsPage. I also would like to create very local subcomponents such as <entry> without having to worry about name clashes elsewhere in my app.

Is there no way of making a sub-component available only within a specific component? Alternatively, should I make turn every component with sub-components into a module?

like image 477
Bjorkegeek Avatar asked Jun 29 '26 13:06

Bjorkegeek


1 Answers

You can move Contact and ContactsPage to a ContactsModule NgModule, and then add this module to imports of modules where you want to use ContactsPage

like image 165
Günter Zöchbauer Avatar answered Jul 01 '26 02:07

Günter Zöchbauer