如何将类添加到 <body>在 Angular Universal 中? [英] How to add class to <body> in Angular Universal?
问题描述
我们可以在 Angular 应用程序的浏览器版本中使用 Rendered2
,但它需要 document.body
引用,并且它在服务器渲染上不可用.
We can user Rendered2
in browser version of Angular app but it requires document.body
reference and it's not available on server rendering.
platform-browser
有 Title
和 Meta
组件,但我想对 有更多的控制code> 部分,比如注入
标签,访问
body
元素等
platform-browser
has Title
and Meta
components but I would like to have more control over <head>
section, like injecting <script>
tags, accessing body
element, etc.
有办法吗?
推荐答案
实际上,Angular Universal 使用 DOMINO引擎盖下的库,它以与浏览器相同的方式创建 document
.唯一的区别是你必须像这样注入 DOCUMENT
:
Actually, Angular Universal uses DOMINO library under the hood, which creates document
in the same way as browser does. The only difference is that you have to inject DOCUMENT
like this:
import { DOCUMENT } from '@angular/common';
import { Component, Inject, Renderer2 } from '@angular/core';
@Component({ ... })
export class AppComponent {
constructor(@Inject(DOCUMENT) document, r: Renderer2) {
r.addClass(document.body, 'myclass');
}
}
这篇关于如何将类添加到 <body>在 Angular Universal 中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!