Revert "refactor(router): remove deprecated initialNavigation
options (#18781)"
This reverts commit d76761bf0136e03dd998e614f0ddd74393bd6332.
This commit is contained in:
parent
7236095f6f
commit
a80ecf6a77
@ -1,7 +1,7 @@
|
|||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<base href="/index.3.html">
|
<base href="/">
|
||||||
<title>NgModule - Contact</title>
|
<title>NgModule - Contact</title>
|
||||||
<meta charset="UTF-8">
|
<meta charset="UTF-8">
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||||
|
@ -218,14 +218,24 @@ export function provideRoutes(routes: Routes): any {
|
|||||||
* The bootstrap is blocked until the initial navigation is complete.
|
* The bootstrap is blocked until the initial navigation is complete.
|
||||||
* * 'disabled' - the initial navigation is not performed. The location listener is set up before
|
* * 'disabled' - the initial navigation is not performed. The location listener is set up before
|
||||||
* the root component gets created.
|
* the root component gets created.
|
||||||
|
* * 'legacy_enabled'- the initial navigation starts after the root component has been created.
|
||||||
|
* The bootstrap is not blocked until the initial navigation is complete. @deprecated
|
||||||
|
* * 'legacy_disabled'- the initial navigation is not performed. The location listener is set up
|
||||||
|
* after @deprecated
|
||||||
|
* the root component gets created.
|
||||||
|
* * `true` - same as 'legacy_enabled'. @deprecated since v4
|
||||||
|
* * `false` - same as 'legacy_disabled'. @deprecated since v4
|
||||||
*
|
*
|
||||||
* The 'enabled' option should be used for applications unless there is a reason to have
|
* The 'enabled' option should be used for applications unless there is a reason to have
|
||||||
* more control over when the router starts its initial navigation due to some complex
|
* more control over when the router starts its initial navigation due to some complex
|
||||||
* initialization logic. In this case, 'disabled' should be used.
|
* initialization logic. In this case, 'disabled' should be used.
|
||||||
*
|
*
|
||||||
|
* The 'legacy_enabled' and 'legacy_disabled' should not be used for new applications.
|
||||||
|
*
|
||||||
* @experimental
|
* @experimental
|
||||||
*/
|
*/
|
||||||
export type InitialNavigation = 'enabled' | 'disabled';
|
export type InitialNavigation =
|
||||||
|
true | false | 'enabled' | 'disabled' | 'legacy_enabled' | 'legacy_disabled';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @whatItDoes Represents options to configure the router.
|
* @whatItDoes Represents options to configure the router.
|
||||||
@ -244,7 +254,7 @@ export interface ExtraOptions {
|
|||||||
useHash?: boolean;
|
useHash?: boolean;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Enables/Disables the initial navigation (enabled by default).
|
* Disables the initial navigation.
|
||||||
*/
|
*/
|
||||||
initialNavigation?: InitialNavigation;
|
initialNavigation?: InitialNavigation;
|
||||||
|
|
||||||
@ -322,12 +332,14 @@ export class RouterInitializer {
|
|||||||
const router = this.injector.get(Router);
|
const router = this.injector.get(Router);
|
||||||
const opts = this.injector.get(ROUTER_CONFIGURATION);
|
const opts = this.injector.get(ROUTER_CONFIGURATION);
|
||||||
|
|
||||||
if (opts.initialNavigation === 'disabled') {
|
if (this.isLegacyDisabled(opts) || this.isLegacyEnabled(opts)) {
|
||||||
|
resolve(true);
|
||||||
|
|
||||||
|
} else if (opts.initialNavigation === 'disabled') {
|
||||||
router.setUpLocationChangeListener();
|
router.setUpLocationChangeListener();
|
||||||
resolve(true);
|
resolve(true);
|
||||||
|
|
||||||
} else if (
|
} else if (opts.initialNavigation === 'enabled') {
|
||||||
opts.initialNavigation === 'enabled' || typeof opts.initialNavigation === 'undefined') {
|
|
||||||
router.hooks.afterPreactivation = () => {
|
router.hooks.afterPreactivation = () => {
|
||||||
// only the initial navigation should be delayed
|
// only the initial navigation should be delayed
|
||||||
if (!this.initNavigation) {
|
if (!this.initNavigation) {
|
||||||
@ -360,11 +372,26 @@ export class RouterInitializer {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (this.isLegacyEnabled(opts)) {
|
||||||
|
router.initialNavigation();
|
||||||
|
} else if (this.isLegacyDisabled(opts)) {
|
||||||
|
router.setUpLocationChangeListener();
|
||||||
|
}
|
||||||
|
|
||||||
preloader.setUpPreloading();
|
preloader.setUpPreloading();
|
||||||
router.resetRootComponentType(ref.componentTypes[0]);
|
router.resetRootComponentType(ref.componentTypes[0]);
|
||||||
this.resultOfPreactivationDone.next(null !);
|
this.resultOfPreactivationDone.next(null !);
|
||||||
this.resultOfPreactivationDone.complete();
|
this.resultOfPreactivationDone.complete();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private isLegacyEnabled(opts: ExtraOptions): boolean {
|
||||||
|
return opts.initialNavigation === 'legacy_enabled' || opts.initialNavigation === true ||
|
||||||
|
opts.initialNavigation === undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
private isLegacyDisabled(opts: ExtraOptions): boolean {
|
||||||
|
return opts.initialNavigation === 'legacy_disabled' || opts.initialNavigation === false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getAppInitializer(r: RouterInitializer) {
|
export function getAppInitializer(r: RouterInitializer) {
|
||||||
|
@ -90,6 +90,48 @@ describe('bootstrap', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should NOT wait for resolvers to complete when initialNavigation = legacy_enabled',
|
||||||
|
(done) => {
|
||||||
|
@Component({selector: 'test', template: 'test'})
|
||||||
|
class TestCmpLegacyEnabled {
|
||||||
|
}
|
||||||
|
|
||||||
|
@NgModule({
|
||||||
|
imports: [
|
||||||
|
BrowserModule,
|
||||||
|
RouterModule.forRoot(
|
||||||
|
[{path: '**', component: TestCmpLegacyEnabled, resolve: {test: TestResolver}}],
|
||||||
|
{useHash: true, initialNavigation: 'legacy_enabled'})
|
||||||
|
],
|
||||||
|
declarations: [RootCmp, TestCmpLegacyEnabled],
|
||||||
|
bootstrap: [RootCmp],
|
||||||
|
providers: [...testProviders, TestResolver],
|
||||||
|
schemas: [CUSTOM_ELEMENTS_SCHEMA]
|
||||||
|
})
|
||||||
|
class TestModule {
|
||||||
|
constructor(router: Router) {
|
||||||
|
log.push('TestModule');
|
||||||
|
router.events.subscribe(e => log.push(e.constructor.name));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
platformBrowserDynamic([]).bootstrapModule(TestModule).then(res => {
|
||||||
|
const router = res.injector.get(Router);
|
||||||
|
expect(router.routerState.snapshot.root.firstChild).toBeNull();
|
||||||
|
// ResolveEnd has not been emitted yet because bootstrap returned too early
|
||||||
|
expect(log).toEqual([
|
||||||
|
'TestModule', 'RootCmp', 'NavigationStart', 'RoutesRecognized', 'GuardsCheckStart',
|
||||||
|
'GuardsCheckEnd', 'ResolveStart'
|
||||||
|
]);
|
||||||
|
|
||||||
|
router.events.subscribe((e) => {
|
||||||
|
if (e instanceof NavigationEnd) {
|
||||||
|
done();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
it('should not run navigation when initialNavigation = disabled', (done) => {
|
it('should not run navigation when initialNavigation = disabled', (done) => {
|
||||||
@Component({selector: 'test', template: 'test'})
|
@Component({selector: 'test', template: 'test'})
|
||||||
class TestCmpDiabled {
|
class TestCmpDiabled {
|
||||||
@ -120,6 +162,37 @@ describe('bootstrap', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should not run navigation when initialNavigation = legacy_disabled', (done) => {
|
||||||
|
@Component({selector: 'test', template: 'test'})
|
||||||
|
class TestCmpLegacyDisabled {
|
||||||
|
}
|
||||||
|
|
||||||
|
@NgModule({
|
||||||
|
imports: [
|
||||||
|
BrowserModule,
|
||||||
|
RouterModule.forRoot(
|
||||||
|
[{path: '**', component: TestCmpLegacyDisabled, resolve: {test: TestResolver}}],
|
||||||
|
{useHash: true, initialNavigation: 'legacy_disabled'})
|
||||||
|
],
|
||||||
|
declarations: [RootCmp, TestCmpLegacyDisabled],
|
||||||
|
bootstrap: [RootCmp],
|
||||||
|
providers: [...testProviders, TestResolver],
|
||||||
|
schemas: [CUSTOM_ELEMENTS_SCHEMA]
|
||||||
|
})
|
||||||
|
class TestModule {
|
||||||
|
constructor(router: Router) {
|
||||||
|
log.push('TestModule');
|
||||||
|
router.events.subscribe(e => log.push(e.constructor.name));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
platformBrowserDynamic([]).bootstrapModule(TestModule).then(res => {
|
||||||
|
const router = res.injector.get(Router);
|
||||||
|
expect(log).toEqual(['TestModule', 'RootCmp']);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
it('should not init router navigation listeners if a non root component is bootstrapped',
|
it('should not init router navigation listeners if a non root component is bootstrapped',
|
||||||
(done) => {
|
(done) => {
|
||||||
@NgModule({
|
@NgModule({
|
||||||
|
Loading…
x
Reference in New Issue
Block a user