dimanche 5 août 2018

Centralizing routes into a Service in Angular

This question focuses on how to manage routing in large-scale apps. I'm still unsure how to handle this, or if tutorials are just far beyond best practices.

Well, let's get to the point. When an application grows, sooner or later there are hundreds of routerLink directives with hard-coded strings like

<a routerLink="foo"> 

or dynamic ones something like that

<a [routerLink]="'/foo/bar/' + myObj.id">

That permanent repetition of hard-coded route fragments and dynamic assembly of routes within the directive doesn't really feel right.

In terms of Don't repeat yourself and Single responsibility principle (SRP) the first thing that comes to my mind is creating something like a RouteService that centralizes those routes and can be injected into components. It might also provide constants to the routing modules.

So, although the Angular style guide preaches to obey SRP this issue is never addressed. And tutorials like the Tour of Heroes are typically much too small to take issues into account that occur when writing large-scale applications.

Are there any caveats of using an injectable Service to create routes or are there any patterns, concepts, frameworks that aim to extract hard-coded routes out of components/templates?

Aucun commentaire:

Enregistrer un commentaire