Is it good practice to have same names and selector for components?
For example:
products ->
list.component.html
list.component.ts // class ListComponent
users ->
list.component.html
list.component.ts // class ListComponent
Or better use this style:
products ->
products-list.component.html
products-list.component.ts // class ProductsListComponent
users ->
users-list.component.html
users-list.component.ts // class UsersListComponent
Two or more components use the same element selector. Because there can only be a single component associated with an element, selectors must be unique strings to prevent ambiguity for Angular.
You can use same directives/components in multiple modules without errors.
The @Component decorator identifies the class immediately below it as a component class, and specifies its metadata. In the example code below, you can see that HeroListComponent is just a class, with no special Angular notation or syntax at all.
It's not a good practice because let's say you have a components.module.ts
file where you import all of your component's modules, you will get multiple module imports which shares the same name.
Which renders it impossible to import all of the modules with the same name (unless you use the as
syntax to "rename" them).
The only scenario I can think of where this is acceptable is for feature modules (pages/routes), where you specify a path to the correct module (if using lazy loading).
But for general components, stay away from it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With