fix(router): do not finish bootstrap until all the routes are resolved (#14327)

Fixes #12162
This commit is contained in:
Victor Savkin
2017-02-09 14:59:08 -05:00
committed by Miško Hevery
parent 74cb575219
commit 541de26f7e
11 changed files with 300 additions and 156 deletions

View File

@ -5,14 +5,14 @@
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {APP_BOOTSTRAP_LISTENER, ApplicationRef, OpaqueToken} from '@angular/core';
import {ExtraOptions, ROUTER_CONFIGURATION, ROUTER_INITIALIZER, Router, RouterPreloader} from '@angular/router';
import {APP_BOOTSTRAP_LISTENER, ComponentRef, OpaqueToken} from '@angular/core';
import {Router} from '@angular/router';
import {UpgradeModule} from '@angular/upgrade/static';
/**
* @whatItDoes Creates an initializer that in addition to setting up the Angular 2
* @whatItDoes Creates an initializer that in addition to setting up the Angular
* router sets up the ngRoute integration.
*
* @howToUse
@ -35,38 +35,17 @@ import {UpgradeModule} from '@angular/upgrade/static';
* @experimental
*/
export const RouterUpgradeInitializer = {
provide: ROUTER_INITIALIZER,
useFactory: initialRouterNavigation,
deps: [UpgradeModule, ApplicationRef, RouterPreloader, ROUTER_CONFIGURATION]
provide: APP_BOOTSTRAP_LISTENER,
multi: true,
useFactory: locationSyncBootstrapListener,
deps: [UpgradeModule]
};
/**
* @internal
*/
export function initialRouterNavigation(
ngUpgrade: UpgradeModule, ref: ApplicationRef, preloader: RouterPreloader,
opts: ExtraOptions): Function {
return () => {
if (!ngUpgrade.$injector) {
throw new Error(`
RouterUpgradeInitializer can be used only after UpgradeModule.bootstrap has been called.
Remove RouterUpgradeInitializer and call setUpLocationSync after UpgradeModule.bootstrap.
`);
}
const router = ngUpgrade.injector.get(Router);
const ref = ngUpgrade.injector.get(ApplicationRef);
router.resetRootComponentType(ref.componentTypes[0]);
preloader.setUpPreloading();
if (opts.initialNavigation === false) {
router.setUpLocationChangeListener();
} else {
router.initialNavigation();
}
setUpLocationSync(ngUpgrade);
};
export function locationSyncBootstrapListener(ngUpgrade: UpgradeModule) {
return () => { setUpLocationSync(ngUpgrade); };
}
/**
@ -77,7 +56,14 @@ export function initialRouterNavigation(
*
* @experimental
*/
export function setUpLocationSync(ngUpgrade: UpgradeModule): void {
export function setUpLocationSync(ngUpgrade: UpgradeModule) {
if (!ngUpgrade.$injector) {
throw new Error(`
RouterUpgradeInitializer can be used only after UpgradeModule.bootstrap has been called.
Remove RouterUpgradeInitializer and call setUpLocationSync after UpgradeModule.bootstrap.
`);
}
const router: Router = ngUpgrade.injector.get(Router);
const url = document.createElement('a');
@ -86,4 +72,4 @@ export function setUpLocationSync(ngUpgrade: UpgradeModule): void {
url.href = next;
router.navigateByUrl(url.pathname);
});
}
}