Angular Universal html lang 标签 [英] Angular Universal html lang tag

查看:37
本文介绍了Angular Universal html lang 标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 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-b​​rowser 中的 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆