fix(router): do not finish bootstrap until all the routes are resolved (#15121)
Cherry-pick of 5df998d086
onto 2.4.x branch.
DEPRECATION:
Use `RouterModule.forRoot(routes, {initialNavigation: 'enabled'})` instead of
`RouterModule.forRoot(routes, {initialNavigtaion: true})`.
Before doing this, move the initialization logic affecting the router
from the bootstrapped component to the boostrapped module.
Similarly, use `RouterModule.forRoot(routes, {initialNavigation: 'disabled'})`
instead of `RouterModule.forRoot(routes, {initialNavigation: false})`.
Deprecated options: 'legacy_enabled', `true` (same as 'legacy_enabled'),
'legacy_disabled', `false` (same as 'legacy_disabled').
The "Router Initial Navigation" design document covers this change.
Read more here:
https://docs.google.com/document/d/1Hlw1fPaVs-PCj5KPeJRKhrQGAvFOxdvTlwAcnZosu5A/edit?usp=sharing
This commit is contained in:

committed by
Chuck Jazdzewski

parent
e5c9bbcbdd
commit
34403cda60
@ -6,8 +6,8 @@
|
||||
* 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';
|
||||
|
||||
|
||||
@ -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');
|
||||
|
||||
|
Reference in New Issue
Block a user