如何在Angular应用程序中使用路由更改页面标题?

Vis*_*ati 10 angular angular5

是否有任何npm模块/ React-Helmet等其他方式允许我们在通过Angular应用程序时更改页面标题?

PS:我正在使用Angular 5.

Fel*_*lix 44

在Angular v14中,有一个内置的策略服务,用于根据主路由器出口从路由中收集标题,并设置浏览器的页面标题。

import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { AboutComponent } from './about.component';
import { HomeComponent } from './home.component';

const routes: Routes = [
  {
    path: 'home',
    component: HomeComponent,
    title: "'My App - Home' // <-- Page title"
  },
  {
    path: 'about',
    component: AboutComponent,
    title: "'My App - About Me'  // <-- Page title"
  }
];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})
export class AppRoutingModule { }
Run Code Online (Sandbox Code Playgroud)

这里


Joh*_*ohn 32

您在Angular中有一个TitleService 5.在组件的构造函数中注入它,并使用该setTitle()方法.

import {Title} from "@angular/platform-browser";

....

constructor(private titleService:Title) {
  this.titleService.setTitle("Some title");
}
Run Code Online (Sandbox Code Playgroud)

以下是Angular的文档:https://v2.angular.io/docs/ts/latest/cookbook/set-document-title.html

  • 如果您只是在构造函数中使用 titleService,则可以省略“private”和“this”,因此停止它添加到组件“this”值 (7认同)

小智 14

这是在 Angular 8 上设置页面标题的经过测试的方法,但您也可以在 Angular 5 上使用它。设置后,您只需在路由文件上设置标题,所有内容都会自动设置。

import { Component } from '@angular/core';
import { ActivatedRoute, Router, NavigationEnd } from '@angular/router';
import { Title } from '@angular/platform-browser';
import { filter, map } from "rxjs/operators";

@Component({
    selector: 'app-root',
    templateUrl: './app.component.html'
})
export class AppComponent {

    constructor (private router: Router, private activatedRoute:    ActivatedRoute, private titleService: Title) {
        this.router.events.pipe(
            filter(event => event instanceof NavigationEnd),
            map(() => {
                let child = this.activatedRoute.firstChild;
                while (child) {
                    if (child.firstChild) {
                        child = child.firstChild;
                    } else if (child.snapshot.data &&    child.snapshot.data['title']) {
                        return child.snapshot.data['title'];
                    } else {
                        return null;
                    }
                }
                return null;
            })
        ).subscribe( (data: any) => {
            if (data) {
                this.titleService.setTitle(data + ' - Website Name');
            }
        });
    }

}
Run Code Online (Sandbox Code Playgroud)

在路由文件上,您可以执行以下操作:

const routes: Routes = [
    {
        path: 'dashboard',
        component: DefaultDashboardComponent,
        data: {
            title: 'Dashboard'
        }
    }
];
Run Code Online (Sandbox Code Playgroud)


ima*_*man 9

这是正确的使用方式

export class AppComponent implements OnInit {
  constructor(private router: Router, private titleService: Title) {
  }

  ngOnInit() {
    this.router.events
      .filter((event) => event instanceof NavigationEnd)
      .map(() => this.router)
      .subscribe((event) => {
          const title = this.getTitle(this.router.routerState, this.router.routerState.root).join(' | ');
          this.titleService.setTitle(title);
        }
      );

  }

  getTitle(state, parent) {
    const data = [];
    if (parent && parent.snapshot.data && parent.snapshot.data.title) {
      data.push(parent.snapshot.data.title);
    }

    if (state && parent) {
      data.push(... this.getTitle(state, state.firstChild(parent)));
    }
    return data;
  }


}
Run Code Online (Sandbox Code Playgroud)