Skip to content

Commit 6a88bad

Browse files
atscottalxhub
authored andcommitted
fix(router): Ensure ActivatedRouteSnapshot#title has correct value (angular#47481)
ActivatedRouteSnapshot data gets mutated in the resolve phase of the Router. The title is assigned as part of this. As a result, the title must be a getter in order to pick up the value that was note available during the class creation. fixes angular#47459 BREAKING CHANGE: The title property is now required on ActivatedRouteSnapshot PR Close angular#47481
1 parent 002ee32 commit 6a88bad

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

goldens/public-api/router/index.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ export class ActivatedRouteSnapshot {
7474
queryParams: Params;
7575
get root(): ActivatedRouteSnapshot;
7676
readonly routeConfig: Route | null;
77-
readonly title?: string;
77+
get title(): string | undefined;
7878
// (undocumented)
7979
toString(): string;
8080
url: UrlSegment[];

packages/router/src/router_state.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,11 @@ export class ActivatedRouteSnapshot {
308308
_queryParamMap!: ParamMap;
309309

310310
/** The resolved route title */
311-
readonly title?: string = this.data?.[RouteTitleKey];
311+
get title(): string|undefined {
312+
// Note: This _must_ be a getter because the data is mutated in the resolvers. Title will not be
313+
// available at the time of class instantiation.
314+
return this.data?.[RouteTitleKey];
315+
}
312316

313317
/** @internal */
314318
constructor(

packages/router/test/page_title_strategy_spec.ts

+13-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import {DOCUMENT} from '@angular/common';
1010
import {Component, Inject, Injectable, NgModule} from '@angular/core';
1111
import {fakeAsync, TestBed, tick} from '@angular/core/testing';
12-
import {Router, RouterModule, RouterStateSnapshot, TitleStrategy} from '@angular/router';
12+
import {NavigationEnd, Router, RouterModule, RouterStateSnapshot, TitleStrategy} from '@angular/router';
1313

1414
import {provideRouterForTesting} from '../testing/src/provide_router_for_testing';
1515

@@ -99,6 +99,18 @@ describe('title strategy', () => {
9999
tick();
100100
expect(document.title).toBe('resolved title');
101101
}));
102+
103+
it('can get the title from the ActivatedRouteSnapshot', async () => {
104+
router.resetConfig([
105+
{
106+
path: 'home',
107+
title: 'My Application',
108+
component: BlankCmp,
109+
},
110+
]);
111+
await router.navigateByUrl('home');
112+
expect(router.routerState.snapshot.root.firstChild!.title).toEqual('My Application');
113+
});
102114
});
103115

104116
describe('custom strategies', () => {

0 commit comments

Comments
 (0)