我需要把页面标题写入一个变量。 我使用打字稿代码:如何使用打字稿获取页面标题?
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;
}
}
但我正在逐渐“标题是未定义”在控制台上。
我也试过:
title: string;
ngOnInit() {
this.title === this.titleService.getTitle();
console.log("Title is " + this.title);
}
public constructor(private titleService: Title) { }
getTitle() {
this.titleService.getTitle();
}
,但结果是一样的。什么是获得页面标题的正确方法?
但是您是否设置了document.title呢? – Vega
是的,我在_Layoute.cshtml中设置了标题。我正在使用.NET Core,但我不认为它与问题有关,是吗? – kendzi
'==='?使用一个'='。 –