如何将类添加到< body>在Angular Universal中? [英] How to add class to <body> in Angular Universal?

查看:80
本文介绍了如何将类添加到< body>在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具有TitleMeta组件,但是我想对<head>部分有更多的控制,例如注入<script>标记,访问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');
    }
}

这篇关于如何将类添加到&lt; body&gt;在Angular Universal中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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