在主模块下
constructor(
private router: Router,
private activatedRoute: ActivatedRoute,
) {}
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) => this.titleService.setTitle(event['title']));
路由设置
{ path: 'test', component: TestComponent, data: { title: '测试标题' } },