5

Screenshot of routing code

How can I set a dynamic page title in Angular 8 using routing data in component?

Community
  • 1
  • 1
Led Del
  • 61
  • 2
  • 4
  • 2
    Does this answer your question? [How to change title of a page using angular(angular 2 or 4) route?](https://stackoverflow.com/questions/44000162/how-to-change-title-of-a-page-using-angularangular-2-or-4-route) – Basel Issmail Jan 08 '20 at 17:55

1 Answers1

9

You can access the route's data property from the snapshot like this:

import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { Title } from '@angular/platform-browser';

@Component({
    templateUrl: './app/home/welcome.component.html'
})
export class WelcomeComponent implements OnInit {
    public pageTitle: string;

    constructor(private titleService: Title, private route: ActivatedRoute) {
      this.titleService.setTitle(this.route.snapshot.data['title']);
    } 

}

This requires injecting the ActivatedRoute, then accessing the route's snapshot. You can use the snapshot instead of subscribing since the data is static.

See this link for titleService in Angular : https://angular.io/guide/set-document-title.

Hope this helps.

Dmitry Grinko
  • 13,806
  • 14
  • 62
  • 86
Vijay
  • 171
  • 2
  • 11