Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should routed components have selectors in Angular?

Tags:

angular

It appears that it is not mandatory to have selectors defined at all for the components that are not referenced by (included into the markup of) other components, i.e. those usually displayed by router (<router-outlet>).

I noticed that if you do give them selector tag, they appear with that tag in the generated HTML like this:

<app-root>
  <router-outlet>
    <app-dashboard>
      ...

But if you don't, the ng-component default is used:

<app-root>
  <router-outlet>
    <ng-component>
      ...

Notice that ng-component was inserted instead of app-dashboard.

My question is: is there any other difference whether you provide the selector for such (router-only) component? I guess not, but couldn't find authoritative reference.

And if there are no functional differences, what would be the preferred approach to use, i.e. what would better express intent / be more semantically correct: to have selectors defined or to omit them in such case?

like image 580
Titan Avatar asked Jan 24 '26 14:01

Titan


1 Answers

To get more semantic HTML for debugging, you can use a selector tag, but for other cases, less is more, so omitting tag seems to be preferable. This way it's clear for yourself and other developers that the component you are dealing with is an end-point of a router path

like image 84
Poul Kruijt Avatar answered Jan 26 '26 13:01

Poul Kruijt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!