how to change page title in angular2 router

后端 未结 14 1243
深忆病人
深忆病人 2020-12-04 16:17

I am trying to change the page title from the router, can this be done?

import {RouteConfig} from \'angular2/router\';
@RouteConfig([
  {path: \'/home\', com         


        
14条回答
  •  北海茫月
    2020-12-04 16:54

    Angular 6+ I have modify the old code using new Pipe() and its working fine.

    import { Title } from '@angular/platform-browser';
    import { filter, map, mergeMap } from 'rxjs/operators';
    

    ...

    constructor(
        private router: Router,
        public activatedRoute: ActivatedRoute,
        public titleService: Title,
      ) {
        this.setTitle();
      }
    

    ....

    setTitle() {
      this.router.events.pipe(
        filter((event) => event instanceof NavigationEnd),
        map(() => this.activatedRoute),
        map((route: any) => {
          while (route.firstChild) route = route.firstChild;
          return route;
        }),
        filter((route) => route.outlet === 'primary'),
        mergeMap((route: any) => route.data)).subscribe((event) => {
          this.titleService.setTitle(event['title']);
          console.log('Page Title', event['title']);
        })
      }
    

提交回复
热议问题