Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(router): Ensure ActivatedRouteSnapshot#title has correct value #47481

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion goldens/public-api/router/index.md
Expand Up @@ -74,7 +74,7 @@ export class ActivatedRouteSnapshot {
queryParams: Params;
get root(): ActivatedRouteSnapshot;
readonly routeConfig: Route | null;
readonly title?: string;
get title(): string | undefined;
// (undocumented)
toString(): string;
url: UrlSegment[];
Expand Down
6 changes: 5 additions & 1 deletion packages/router/src/router_state.ts
Expand Up @@ -308,7 +308,11 @@ export class ActivatedRouteSnapshot {
_queryParamMap!: ParamMap;

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

/** @internal */
constructor(
Expand Down
14 changes: 13 additions & 1 deletion packages/router/test/page_title_strategy_spec.ts
Expand Up @@ -9,7 +9,7 @@
import {DOCUMENT} from '@angular/common';
import {Component, Inject, Injectable, NgModule} from '@angular/core';
import {fakeAsync, TestBed, tick} from '@angular/core/testing';
import {Router, RouterModule, RouterStateSnapshot, TitleStrategy} from '@angular/router';
import {NavigationEnd, Router, RouterModule, RouterStateSnapshot, TitleStrategy} from '@angular/router';

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

Expand Down Expand Up @@ -99,6 +99,18 @@ describe('title strategy', () => {
tick();
expect(document.title).toBe('resolved title');
}));

it('can get the title from the ActivatedRouteSnapshot', async () => {
router.resetConfig([
{
path: 'home',
title: 'My Application',
component: BlankCmp,
},
]);
await router.navigateByUrl('home');
expect(router.routerState.snapshot.root.firstChild!.title).toEqual('My Application');
});
});

describe('custom strategies', () => {
Expand Down