I have an Angular application with module-per-feature architecture. I have an OrdersModule
with components like ListOfOrders
, DetailOfOrder
or EditOfOrder
All of these components require OrdersApiService
in constructor, which is basically a simple service that provides CRUD operations over HTTPS using REST API backend.
The OrdersModule
works with an entity called Order
I had received a feature request where I will implement new entity, called a Draft Order. Practically there is no difference between Draft Order and Order (they contain the very same properties, they are created in the very same way) but they have to be stored differently, they have different API endpoint (myApi.com/orders
vs myApi.com/draft-orders
)
The easiest way (from time spent perspective) is to copy/paste Orders to new module. However, I would love to avoid code duplication, so this approach should work for me:
{
path: 'orders',
loadChildren: () => import('./orders/orders.module').then(m => m.OrdersModule)
},
{
path: 'draft-orders',
loadChildren: () => import('./orders/orders.module').then(m => m.OrdersModule)
},
Now I would need to inject different service to components according to routing. I know about the workaround
{
path: 'orders',
loadChildren: () => import('./orders/orders.module').then(m => m.OrdersModule),
data: {
type: 'orders'
}
},
{
path: 'draft-orders',
loadChildren: () => import('./orders/orders.module').then(m => m.OrdersModule),
data: {
type: 'draft-orders'
}
},
and in service consume ActivatedRoute:
constructor(
private route: ActivatedRoute,
) {
this.type = this.route.snapshot.data['type'];
}
getOrders() {
return this.http.get('myApi.com/' + this.type);
}
Is there any a solution how to inject whole service?
Thanks!