Angular 2 Renderer2 - 它是如何工作的 [英] Angular 2 Renderer2 - how it works

查看:40
本文介绍了Angular 2 Renderer2 - 它是如何工作的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图了解在指令或 angular 2 组件中添加 renderer2 装饰的必要性.

I'm trying to understand the necessity of adding the renderer2 decoration in directives or in component in angular 2.

来自文档:渲染器 2 文档他们没有提供锄头工作的例子.谁能用完整的例子解释一下.请

from documentaion :renderer 2 documentaion they do not provide example that hoe it works. can anyone explain with full example.please

推荐答案

Renderer2 只是一个抽象.如果您使用 Universal(服务器端渲染)或 WebWorkers,则没有可用的 DOM,直接访问 DOM 的代码只会导致异常.

Renderer2 is just an abstraction. If you use Universal (server-side-rendering) or WebWorkers, there is no DOM available and code that accesses the DOM directly will just cause an exception.

如果您使用 Renderer2,Angular 可以使用其依赖注入功能提供不同的实现,以使您的代码使用与当前运行代码的平台兼容的实现.

If you use Renderer2, Angular can provide different implementations using its dependency injection functionality, to make your code use an implementation that is compatible with the platform the code is currently running on.

Renderer2 非常有限,因为所有方法都只允许更新 DOM,但没有一个允许从 DOM 中读取任何信息.如果你真的需要阅读你需要采取特殊的措施使其与Universal或WebWorker一起工作(例如检查当前平台,如果不是浏览器平台,则跳过直接访问DOM的代码的执行,或者使用其他提供的当前平台)

Renderer2 is quite limited, because all methods only allow to update the DOM, but none allows to read any information from the DOM. If you actually need to read you need to take special measures to make it work with Universal or WebWorker (for example check the current platform and skip the execution of code that directly accesses the DOM if it's not the browser platform, or use others provided by the current platform)

这篇关于Angular 2 Renderer2 - 它是如何工作的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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