Issue 1:
I'm submitting a ...

[x] bug report => search github for a similar issue or PR before submitting
[ ] feature request
[ ] support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question

Current behavior

What we're trying to achieve:

We have implemented a resolve guard in a lazily loaded module that will navigate to an error component on failure to retrieve data.

The resolve guard will also store the failure message in a field of itself.

The error component takes the resolve guard as a dependency, and displays the error message from it.

Our implementation is very similar to the Tour of Heroes example with the resolve guard navigating to another page on failure. See this plunkr example, and click on any crisis under the Crisis Center

We have produced an example of the problem by extending the Tour of Heroes example.

When CrisisCenterModule is lazily loaded, we have observed:

The resolver instance for CrisisDetailComponent is different to the instance being injected in to ErrorComponent, which is a sibling route to the activate route. We have confirmed that the resolver is only provided inside the CrisisCenterRoutingModule, which is inside the lazily loaded module.

We can see that the CrisisCenterModule and all it's contained modules and components are instantiated twice, with different injectors.

When CrisisCenterModule module is not lazily loaded, this problem does not occur.

This was previously working when we were using Angular 2.1.0, until we upgraded to 2.3.1/2.4.1. We narrowed it down to when the version of @angular/router changed from 3.1.0 to 3.2.0. This problem did not occur with the beta and RC versions of 3.2.0.

We did consider using an intermediate service for the resolver to share the error message to another component, but we experienced the same issue with multiple instances of this service.

Expected behavior

Clicking on a crisis under the Crisis Center in this plunkr example, you will see Error component - error message:, whereas we expect to see Error component - error message: error.

We created another plunkr example where CrisisCenterModule module is not lazily loaded, and the problem does not occur.

Minimal reproduction of the problem with instructions

We have modified Tour of Heroes example in the following ways:

app/crisis-center/crisis-detail-resolver.service.ts - Modified to always set the errorMessage field, and navigate to the ErrorComponent.

app/crisis-center/error.component.ts- Newly added. Displays the errorMessage field from the injected CrisisDetailResolver.

You can download this example Github project where the @angular/router version is 3.2.0. To run:

npm install

npm run

Open http://localhost:4200

Click on a crisis under the Crisis Center

You will see Error component - This is the error message:, which is missing the error message from the resolver.

You can see in the console log that the CrisisCenterModule is instantiated twice with different injectors, and the error message is not displayed.

To see it working, you must switch the version of @angular/router to 3.1.0 in packages.json, clear the npm_modules folder, and re-run the above steps. You should see the following displayed: Error component - This is the error message:I failed to load crisis. You will also see in the console log that components are only being initialised once.

Alternatively, you can reproduce with this plunkr example.

What is the motivation / use case for changing the behavior?

We want the resolver to handle failure to load data and navigate to a component that can handle this failure appropriately. This seems to be the pattern that is shown in the Tour of Heroes example (from the Routing advanced guide).

The alternative would be to modify the resolver to wrap failures in a result object, and let the activated route component handle success and failure cases. We'd like to avoid this, as it would make the component more complex.

Please tell us about your environment:

Mac OS X 10.11.5

Angular version: 2.4.1. @angular/router at 3.4.1. Works when router is at 3.1.0.

Browser: Chrome 55.0.2883.95

Language: TypeScript 2.0.3

Node (for AoT issues): node --version = 6.2.1

Issue 2:
I'm submitting a ... (check one with "x")

[x] bug report => search github for a similar issue or PR before submitting
[ ] feature request
[ ] support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question

Current behavior

After upgrading to router 3.2.0, services being provided by component module are being instantiated twice. Once while resolver is being run (last place in router is PreActivation.prototype.getToken) and then again later during activation (last place in router is RouterOutlet.prototype.activate).

Upgrading rest of Angular to 2.2.0 and leaving router at 3.1.1 it works as it used to (and looking in debugger I see service is only instantiated from PreActivation.prototype.getToken)

Expected behavior

Providers should only be instantiated once - as they are when using router version 3.1.1

Minimal reproduction of the problem with instructions

Haven't yet reproduced in a plnkr but my use case has a lazy loaded component that itself has sub components (not lazy loaded). The sub component has a resolver that uses a service, and the sub component itself uses the service. The service is included in the lazy loaded components module providers list and is being instantiated twice. Once apparently while router is working on resolve and once when component is being activated.

What is the motivation / use case for changing the behavior?

Its just wrong, and my service fetches data on initialization so extra instance causes extra fetch. This does not happen with router 3.1.1 (never tried 3.1.2 as deactivate guards don't work correctly in that version)

Please tell us about your environment:

Windows 10, Visual Studio Code, angular-cli: 1.0.0-beta.19-3 ng serve

Angular version: 2.0.X

2.2.0

Browser: [all | Chrome XX | Firefox XX | IE XX | Safari XX | Mobile Chrome XX | Android X.X Web Browser | iOS XX Safari | iOS XX UIWebView | iOS XX WKWebView ]

Only tried in Chrome

Language: [all | TypeScript X.X | ES6/7 | ES5]

Typescript 2.0.9

Node (for AoT issues): node --version =