angular 2 resolver for every view
You basically need one resolver
for each component
and need to be declare once for each route, unless they are parent > child, you can share the resolver between them like below:
const MessagesRoutes: Routes = [
{ path: '', resolve: { messages: EmailResolver }, children: [
{ path: 'inbox', component: InboxComponent },
{ path: 'outbox', component: OutboxComponent },
{ path: 'sent', component: SentComponent },
{ path: 'received', component: ReceivedComponent }
]}
];
Not sure what your question is about but you can use a componentless parent route with a resolver and share this with all its child routes
const MessagesRoutes: Routes = [
{ path: '', resolve: { messages: InboxResolver }, children: [
{ path: 'inbox', component: InboxComponent},
{ path: 'xxx', component: XxxComponent},
{ path: 'zzz', component: XxxComponent},
]}
];
Provided you just want to add the resolver to every top level route then something like the below would do it.
const addGlobalResolver = <T extends Type<Resolve<any>>>(routePaths: Routes, resolver: T, name: string): Routes => {
return routePaths.map(route => {
if (route.resolve != null || route.resolve === undefined) {
route.resolve = {};
}
route.resolve[name] = resolver;
return route;
});
};
Just invoke it before you pass the routes to the routing module.
[RouterModule.forRoot(addGlobalResolver(routes, InjuryResolver, 'Injury'))]