I have a root module that lazily loads another module. In this lazily loaded module, a route has a resolver that tries to load some external data. If it fails, it stores an error message in itself, and navigates to an error component. The error component has the resolver injected, and then should display the error message from the resolver.
I have adapted the official Angular 2 Tour of Heroes as an example in the following ways:
app/crisis-center/crisis-detail-resolver.service.ts- Modified to always set theerrorMessagefield, and navigate to theErrorComponent. This previously navigated to the/crisis-centercommand.app/crisis-center/error.component.ts- Newly added. Takes theCrisisDetailResolveras a dependency, and displays theerrorMessagefrom it.
If the crisis-center.module.ts is lazily loaded, the resolver injected in to ErrorComponent is not the same as the resolver used in the router. I can only confirm this by the absence of errorMessage in the resolver when debugging. You can see this on plnkr by clicking on any crisis under the crisis center. You'll see no error message being displayed.
If the module is not lazily loaded (i.e. I load it explicitly in the root module), the resolver is now a singleton and I have the error message to display in the ErrorComponent. You can see this on plnkr by clicking on any crisis under the crisis center. You'll see an error message displayed.
I'm confused as to why this isn't working, as far as I'm aware, the CrisisDetailResolver is declared to be a singleton in the provider block of app/crisis-center/crisis-center-routing.module.ts.