Use the following order of groups to organize Angular components:
- Injected dependencies.
- Private properties.
- Data binding properties.
- View and content properties.
- UI properties.
- Component API properties.
- Constructor.
- Lifecycle hooks.
- Event handlers.
- Component API methods.
- Private methods.
| Group | Description |
|---|---|
| Component API methods | Public methods intended to be used by other components, directives, or services. |
| Component API properties | Public properties intended to be used by other components, directives, or services. |
| Data binding properties | Properties decorated by Input/Output or initialized with input/output. |
| Event handlers | protected methods used by the component template. |
| Injected dependencies | Services and other dependencies resolved using inject. |
| Lifecycle hooks | Methods implementing the AfterContentChecked, AfterContentInit, AfterViewChecked, AfterViewInit, DoCheck OnChanges, OnDestroy, or OnInit interfaces. |
| Private properties | Properties marked by private or #. |
| UI properties | protected properties used by the component template. |
| View and content properties | Properties decorated by ContentChild/ContentChildren/ViewChild/ViewChildren or initialized with contentChild/contentChildren/viewChild/viewChildren. |
Unfortunately this rule will not fix the ordering automatically and only indicate erroneous ordering. Automatic ordering used to be a thing in now deprecated tslint, but will not arrive in typescipt-eslint. Furthermore the member-ordering rule is frozen and will not accept any extensions.
There is this new, small prettier plugin that does some sorting, but it does not have this level of granularity.
I heard there is a VS Code plugin that might help. IDEA / PhpStorm cannot do this with typescript.
I'd be interested on other tools that can help with this ordering.