import { Component, OnInit } from '@angular/core'; import { Title } from '@angular/platform-browser';
@Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css'] }) export class AppComponent implements OnInit {
title: string = 'My App';
constructor(private titleService: Title) {}
ngOnInit() { this.titleService.setTitle(this.title); } }
import { Component, OnInit } from '@angular/core'; import { Title } from '@angular/platform-browser';
@Component({ selector: 'app-home', templateUrl: './home.component.html', styleUrls: ['./home.component.css'] }) export class HomeComponent implements OnInit {
constructor(private titleService: Title) { }
ngOnInit() { this.titleService.setTitle('Home | My App'); }
}
注意:页面标题的设置需要在Angular的Zone之外执行。相应地,Title服务会自动执行NgZone.run()。因此,可以在页面标题的设置中不需要再手动调用NgZone。
下一篇:Angular中的动态插件