angular2+ 里默认切换路由或者切换组件,页面的标题是不会变化的。 如果想针对每个路由设置页面标题,要使用 Title 服务,我们需要从 @angular/platform-browser 库导入 Title 类,然后利用 Angular 依赖注入的机制,通过构造注入的方式注入 Title 服务。其Title Service 提供了setTitle()和getTitle()方法。
1. route设置页面
import {
Routes } from '@angular/router';
import {
HeroesComponent } from './heroes/heroes.component';
import {
HomeComponent } from './home/home.component'
export const rootRouterConfig: Routes = [
{
path: '', redirectTo: 'home'