Expose resolved route title
See original GitHub issueWhich @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 withTitleStrategy
token. To get the actual strategy used, one must retrieve it fromRouter.titleStrategy
.- This is inconsistent with existing public api of
ActivatedRoute
/ActivatedRouteSnapshot
Issue Analytics
- State:
- Created a year ago
- Reactions:6
- Comments:9 (8 by maintainers)
Top GitHub Comments
@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
andActivatedRouteSnapshot
, it’s still good to have concrete reasoning.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:
Resolve<string>
this is not easyTitleStrategy
and callgetResolvedTitleForRoute
, 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)
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)