关于angular 的路由title设置

时间:2023-12-22 09:27:50

在主模块下

  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: '测试标题' } },