Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Same names for components with Angular

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
like image 378
mystdeim Avatar asked May 25 '17 18:05

mystdeim


People also ask

Can two components have same name in Angular?

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.

Can we create same name component different modules?

You can use same directives/components in multiple modules without errors.

What is @component called in Angular?

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.


1 Answers

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.

like image 94
Chrillewoodz Avatar answered Oct 06 '22 02:10

Chrillewoodz