Can someone help me with how to access the page title and save it to a variable using TypeScript in an Angular project?
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'home',
templateUrl: './home.component.html'
})
export class HomeComponent implements OnInit {
title: string;
ngOnInit() {
console.log("Title is " + this.title);
this.title === document.title;
}
}
However, I keep receiving "Title is undefined" in the console.
I also tried:
title: string;
ngOnInit() {
this.title === this.titleService.getTitle();
console.log("Title is " + this.title);
}
public constructor(private titleService: Title) { }
getTitle() {
this.titleService.getTitle();
}
Unfortunately, the outcome remains unchanged. What is the proper way to retrieve the page title?