question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Expose resolved route title

See original GitHub issue

Which @angular/* package(s) are relevant/related to the feature request?

router

Description

Since Angular 14, router has a new mechanism to update page title from route config. The route can either be a static string or resolved in the same way as resolve data.

But the resolved route title is not exposed and inaccessible from client application (it seems the title is actually stored in the resolved Data, but using a private Symbol so it can’t be accessed properly).

Proposed solution

Expose a new title property on ActivatedRoute and ActivatedRouteSnapshot, just like existing params, data

Alternatives considered

Alternative 1: Expose the internal RouteTitle symbol used to store title in Data, but this is probably not wanted because it would expose an implementation detail.

Alternative 2: Use TitleStrategy.buildTitle(snapshot: RouterStateSnapshot) or, better, TitleStrategy.getResolvedTitleForRoute(snapshot: ActivatedRouteSnapshot). But this has following drawbacks:

  • TitleStrategy must be injected where we need to (only) read the route title, which is not its primary purpose…
  • TitleStrategy is not very convenient to inject, because in default case, DefaultTitleStrategy is used but is not available through DI with TitleStrategy token. To get the actual strategy used, one must retrieve it from Router.titleStrategy.
  • This is inconsistent with existing public api of ActivatedRoute/ActivatedRouteSnapshot

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:6
  • Comments:9 (8 by maintainers)

github_iconTop GitHub Comments

2reactions
atscottcommented, Jul 18, 2022

@EmmanuelRoux Thanks, I understand now. I appreciate the detailed explanation. It helps a lot to justify why we make the change. Even though it’s just a 1 liner in each of the ActivatedRoute and ActivatedRouteSnapshot, it’s still good to have concrete reasoning.

1reaction
EmmanuelRouxcommented, Jul 16, 2022

Sorry I was not clear!

I’m speaking here as a library author, so I have no control on the strategy the app developer will define in the app.

That said, at some point I need to know what title has been defined for a given ActivatedRoute.

What I’m saying is: in order to retrieve the title defined by the app developer, there are several choices but none of them seems good for me:

  • I can read in the route config, but because the title may be a Resolve<string> this is not easy
  • I can inject the TitleStrategy and call getResolvedTitleForRoute, just like your example: but this will not work if the user is using the default strategy, except by using something like this: inject(TitleStrategy, InjectFlags.Optional) || inject(DefaultTitleStrategy)
  • I can read the document title (either from DOM or by using Title service): but currently nothing guarantees that a custom title strategy will actually update document title (although it is surely not a very common scenario)
Read more comments on GitHub >

github_iconTop Results From Across the Web

Router tutorial: tour of heroes - Angular
The resolve guard (pre-fetching route data); Lazy loading an NgModule; The canLoad guard (check before loading feature module assets). This guide proceeds as...
Read more >
Angular How to change navmenu header based on route
One possible solution is to listen to router events and based on them decide about what content should be displayed. This approach would...
Read more >
Setting Page Titles Natively With The Angular Router
In Angular v14, there is a built-in strategy service for collecting the title from the route based on the primary router outlet, and...
Read more >
Angular Routing Explained -- newline - Fullstack.io
When a user navigates to /people/bob in the app, the router recognises it as a match with this route, and loads the PersonComponent...
Read more >
An Introduction to Component Routing with Angular Router
When calling the resolve() method, Angular Router conveniently passes in the activated route snapshot and the router state snapshot to provide ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found