how to change page title in angular2 router

后端 未结 14 1238
深忆病人
深忆病人 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:46

    This is what I went with:

    constructor(private router: Router, private title: Title) { }
    
    ngOnInit() {
        this.router.events.subscribe(event => {
            if (event instanceof NavigationEnd) {
                this.title.setTitle(this.recursivelyGenerateTitle(this.router.routerState.snapshot.root).join(' - '));
            }
        });
    }
    
    recursivelyGenerateTitle(snapshot: ActivatedRouteSnapshot) {
        var titleParts = <string[]>[];
        if (snapshot) {
            if (snapshot.firstChild) {
                titleParts = titleParts.concat(this.recursivelyGenerateTitle(snapshot.firstChild));
            }
    
            if (snapshot.data['title']) {
                titleParts.push(snapshot.data['title']);
            }
        }
    
        return titleParts;
    }
    
    0 讨论(0)
  • 2020-12-04 16:49

    In the below example:

    -We added object of data: { title: 'NAME' } to any routing object.

    -We set a basic name ("CTM") for uploading time (when clicking F5 for Refreash..): <title>CTM</title>.

    -We added "TitleService" class.

    -we handle Routher events by filtering them from app.component.ts.

    index.html:

    <!DOCTYPE html>
    <html>
      <head>
        <base href="/">
        <title>CTM</title>
      </head>
    

    ...

    app.module.ts:

    import { NgModule, enableProdMode } from '@angular/core';
    import { BrowserModule  } from '@angular/platform-browser';
    import { TitleService }   from './shared/title.service';
    ...
    
    
    @NgModule({
      imports: [
        BrowserModule,
    ..
      ],
      declarations: [
          AppComponent,
    ...
      ],
      providers: [
          TitleService,
    ...
      ],
      bootstrap: [AppComponent],
    })
    export class AppModule { }
    
    enableProdMode();
    

    title.service.ts:

    import { Injectable } from '@angular/core';
    import { Title }  from '@angular/platform-browser';
    import { ActivatedRouteSnapshot } from '@angular/router';
    
    
    @Injectable()
    export class TitleService extends Title {
    
        constructor() {
            super();
        }
    
    
        private recursivelyGenerateTitle(snapshot: ActivatedRouteSnapshot) {
            var titleParts = <string[]>[];
            if (snapshot) {
                if (snapshot.firstChild) {
                    titleParts = this.recursivelyGenerateTitle(snapshot.firstChild);
                }
    
                if (snapshot.data['title']) {
                    titleParts.push(snapshot.data['title']);
                }
            }
    
            return titleParts;
        }
    
        public CTMGenerateTitle(snapshot: ActivatedRouteSnapshot) {
            this.setTitle("CTM | " + this.recursivelyGenerateTitle(snapshot).join(' - '));
        }
    
    }
    

    app-routing.module.ts:

    import { Injectable } from '@angular/core';
    import { NgModule } from '@angular/core';
    import { Routes, RouterModule } from '@angular/router';
    
    import { MainComponent } from './components/main.component';
    
    import { Router, CanActivate } from '@angular/router';
    import { AuthGuard }          from './shared/auth/auth-guard.service';
    import { AuthService }    from './shared/auth/auth.service';
    
    
    export const routes: Routes = [
      { path: 'dashboard', component: MainComponent, canActivate: [AuthGuard], data: { title: 'Main' } },
    ];
    
    @NgModule({
        imports: [
            RouterModule.forRoot(routes, { useHash: true })  // .../#/crisis-center/
        ],
        exports: [RouterModule],
        providers: [
    ..
        ]
    })
    
    export class AppRoutingModule { }
    
    export const componentsOfAppRoutingModule = [MainComponent];
    

    app.component.ts:

    import { Component } from '@angular/core';
    import { Router, NavigationEnd, ActivatedRouteSnapshot } from '@angular/router';
    import { TitleService } from './shared/title.service';
    
    @Component({
      selector: 'ctm-app',
      template: `
        <!--<a [routerLink]="['/dashboard']">Dashboard</a>
        <a [routerLink]="['/login']">Login</a>
        <a [routerLink]="['/']">Rooting</a>-->
        <router-outlet></router-outlet>
        `
    })
    export class AppComponent {
    
        constructor(private router: Router, private titleService: TitleService) {
    
            this.router.events.filter((event) => event instanceof NavigationEnd).subscribe((event) => {
                console.log("AppComponent.constructor: Setting HTML document's Title");
                this.titleService.CTMGenerateTitle(this.router.routerState.snapshot.root);
            });
    
        }
    
    
    }
    
    0 讨论(0)
  • 2020-12-04 16:50

    The Title service @EricMartinez points out has a setTitle() method - that's all you need to set the title.

    In terms of doing it automatically on route changes, as of now there's no built-in way of doing this other than subscribing to Router and calling setTitle() in your callback:

    import {RouteConfig} from 'angular2/router';
    import {Title} from 'angular2/platform/browser';
    
    @RouteConfig([
      {path: '/home', component: HomeCmp, name: 'HomeCmp' }
    ])
    class MyApp {
        constructor(router:Router, title:Title) {
           router.events.subscribe((event)=>{ //fires on every URL change
              title.setTitle(getTitleFor(router.url));
           });
        }
     }
    

    That said, I emphasize as of now because the router is still under heavy development, and I expect (or at least hope) that we'll be able to do this via RouteConfig in the final release.

    EDIT:

    As of the release of Angular 2 (2.0.0), a few things have changed:

    • The docs for the Title service are now here: https://angular.io/docs/ts/latest/api/platform-browser/index/Title-class.html
    • The service is imported from '@angular/platform-browser'
    0 讨论(0)
  • I can also recommend @ngx-meta/core plugin plugin that I've just released, in the case if you're looking for a method to set page title and meta tags dynamically.

    0 讨论(0)
  • 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']);
        })
      }
    
    0 讨论(0)
  • 2020-12-04 16:56

    Simple generic way to set the title:

    import { Component } from '@angular/core';
    import { Title } from '@angular/platform-browser';
    import { ActivatedRoute, Router, NavigationEnd } from '@angular/router';
    
    @Component({
      selector: 'app-root',
      templateUrl: './app.component.html',
      styleUrls: ['./app.component.scss']
    })
    export class AppComponent {
    
      constructor(
        private activatedRoute: ActivatedRoute,
        private router: Router,
        private titleService: Title
        ) {}
    
      ngOnInit() {
        this.router.events.subscribe(event => {
          if (event instanceof NavigationEnd) {
            const { title } = this.activatedRoute.firstChild.snapshot.data;
            this.titleService.setTitle(title);
          }
        });
      }
    
    }
    

    where title needs to be set on each route like:

    { path: '', component: WelcomeComponent, data: {title: 'Welcome to my app'} }
    
    0 讨论(0)
提交回复
热议问题