question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItΒ collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Improve encapsulation be adding declarations array in @Component metadata

See original GitHub issue

πŸš€ feature request

Relevant Package

This feature request is for @angular/core

Description

Improve component encapsulation while removing cluttering of `NgModule`

Describe the solution you’d like

Inside the `@Component` metadata, add the key: `declarations`, that key should accept an array of components, similar to the api used in `@NgModule`.
@Component({
    selector: 'nz-header',
    template: `
        <header>
            <nz-nav></nz-nav>
        </header>
    `,
    declarations: [NavComponent]
})
export class HeaderComponent { ... }

Describe alternatives you’ve considered

Yes, Feature module and dynamic components

Long description

One of the programming concepts that guides us as developers is encapsulation.
The ability to expose what we need to expose as public, and to hide the rest as private.
Consider the following components, module.

Our module AppModule contains the following components:

  • HeaderComponent
    • NavComponent
  • FooterComponent

HeaderComponent is placing the NavComponent, and the NavComponent will be used only in the HeaderComponent

The NavComponent is a component that should only be used in the HeaderComponent it’s like a private implementation of the HeaderComponent.
Encapsulation rules should not expose NavComponent to the other components inside our module.
So the FooterComponent should not be able to place the NavComponent since it’s private to the HeaderComponent.

Currently to declare the structure above our AppModule would look like this:

@NgModule({
    declarations: [HeaderComponent, NavComponent, FooterComponent]
})
export class AppModule {}

this configuration will allow both the HeaderComponent and the FooterComponent to place the NavComponent.
In my opinion this breaks encapsulation rules and clutters the NgModule with a lot of components which should be private.
The ideal would be that we can declare the module like this:

@NgModule({
    declarations: [HeaderComponent, FooterComponent]
})
export class AppModule {}

And declare the HeaderComponent like this:

@Component({
    selector: 'nz-header',
    template: `
        <header>
            <nz-nav></nz-nav>
        </header>
    `,
    declarations: [NavComponent]
})
export class HeaderComponent { ... }

And of course not allow the FooterComponent to add the selector of the NavComponent.

I can arrange a folder for the HeaderComponent and arrange in that same folder all the private components that header is using.

The result will be:

  • cleaner NgModule with smaller array of components
  • Better encapsulation in the components
  • Better communication to the team regarding what components they can use and what is hidden

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:8
  • Comments:9 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
petebacondarwincommented, Nov 15, 2020

This has definitely been discussed before, but I can’t find a duplicate issue. One of the goals of Ivy was to remove the need for NgModule. So it is definitely on the radar.

0reactions
angular-automatic-lock-bot[bot]commented, Aug 15, 2021

This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

Read more comments on GitHub >

github_iconTop Results From Across the Web

NgModule FAQ - Angular
This page answers the questions many developers ask about NgModule design and implementation. What classes should I add to the declarations array?link. Add...
Read more >
Angular 12, Typecript 4.2 : The class is listed in the ...
1 Answer 1 ; Component Β· selector: 'app-chart', ; NgModule Β· declarations: [ ChartComponent ], ; // Introduced an additional array so it...
Read more >
Angular Views, Routing, and NgModules Explained
It begins by creating a class, decorating it with @Component , and filling in metadata. This approach occurs for any pre-defined component ......
Read more >
LLVM Language Reference Manual
str ”, an external declaration of the β€œ puts ” function, a function definition for β€œ main ” and named metadata β€œ foo...
Read more >
Declaring Components in an Angular Module | Pluralsight
An Angular module is a declaration of all the components that will be ... We can import other modules by adding them to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found