Angular Universal html lang 标签 [英] Angular Universal html lang tag
问题描述
在 Angular Universal 中,我有一个 index.html 文件.在顶部它有
In Angular Universal I have an index.html file. At the top it has
<html lang="en">
我想根据我所在的页面更改此设置.maldonadoattorney.com/es/jailreleases 将是
I would like to change this based on the page I am on. maldonadoattorney.com/es/jailreleases would be
<html lang="es">
maldonadoattorney.com/jailreleases 将是
maldonadoattorney.com/jailreleases would be
<html lang="en">
是否有一种可接受的方式来做到这一点?已阅读其他问题,DOCUMENT 已弃用,因此请谨慎使用.
Is there an accepted way of doing this? Have read other questions and DOCUMENT is deprecated, so am wary of using it.
我的站点地图设置了 hreflang 标签,但希望 html lang 标签正确无误.目前,我正在从 index.html 中删除 lang="en" 标签.
I have my site map set up with hreflang tags, but would like the html lang tag to be correct. Currently, I'm taking the lang="en" tag out of my index.html.
推荐答案
你说得对,但是 @angular/platform-browser
中的 DOCUMENT
已被弃用,取而代之的是 DOCUMENT
来自 @angular/common
.
You're right but DOCUMENT
from @angular/platform-browser
is deprecated in favor of DOCUMENT
from @angular/common
.
所以你可以使用下面的代码:
So you can use the following code:
import { DOCUMENT } from '@angular/common';
...
export class AppComponent implements OnInit {
constructor(@Inject(DOCUMENT) private document: Document) {}
ngOnInit() {
this.document.documentElement.lang = 'es';
}
...
}
这篇关于Angular Universal html lang 标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!