Angularでコンポーネントからページタイトルを変更する方法

Angularでコンポーネントからページタイトルを変更する方法

2018年11月11日
スポンサーリンク

Angularでコンポーネントからページタイトルを変更する方法をご紹介します。

やり方

コンポーネントからページタイトルを変更するにはTitleをDIする必要があります。

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 {

  constructor(
    private title: Title
  ) {}

  ngOnInit() {
    this.title.setTitle('タイトル');
  }
}

ブラウザで確認してみると

無事に変更できました。