Angular 2 Renderer2-工作原理 [英] Angular 2 Renderer2 - how it works

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

问题描述

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

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

来自documentaion:渲染器2文档他们没有提供example头的例子.任何人都可以用完整的例子来解释.请

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的代码的执行,或者使用由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天全站免登陆