One way to do this, as you mentioned in your question, is to sign up for router.event
. Suppose you have a top-level component where you want to display the title. You need to subscribe to router events as follows:
import { Router, NavigationEnd, ActivatedRoute } from '@angular/router';
...
title: string;
constructor(
private router: Router,
private activatedRoute: ActivatedRoute,
) {}
ngOnInit() {
this.router.events
.filter(event => event instanceof NavigationEnd)
.map(() => this.activatedRoute)
.map(route => {
while (route.firstChild) {
route = route.firstChild;
}
return route;
})
.filter(route => route.outlet === 'primary')
.mergeMap(route => route.data)
.subscribe((event: NavigationEnd) => {
this.title = event['title'] || 'Default Title';
});
}
, , : NavigationEnd
. , data
param, .
:
{
path: 'home',
component: HomeComponent,
data: { title: 'Home Title' },
}
, . Observables
, , - , .
document.title
, document.title
Angular Title
, :
import { Title } from '@angular/platform-browser';
...
constructor(private titleService: Title) {}
...
this.titleService.setTitle('Home Title');