简体   繁体   English

在 angular5 中使用纯 JavaScript

[英]Use of Plain JavaScript in angular5

angular5项目中使用JavaScript(类似于getDocumentById语法)是一种良好的编码习惯,或者我们应该使用ElementRefTemplateRefContainerRef来引用DOM元素而不是ElementRef JavaScript。

It's not encouraged to use the DOM element manipulation directly .不鼓励直接使用 DOM 元素操作。 This blog explains why it should be avoided 这个博客解释了为什么应该避免它

With Angular 6 last update by google they have started moving towards shadow dom and dom hierarchy tree it would help dev community to not worrying about actual dom elements manipulation随着谷歌的 Angular 6 最后一次更新,他们已经开始转向shadow dom和 dom 层次树,这将帮助开发社区不用担心实际的 dom 元素操作

Use the framework, don't go direct to the DOM (which is what you mean by "plain JavaScript") unless you have no other choice.使用框架,除非别无选择,否则不要直接使用 DOM(这就是“纯 JavaScript”的意思)。 (Or don't use a framework at all.) (或者根本不使用框架。)

But note the warning in the ElementRef docs :但请注意ElementRef文档中的警告:

Permitting direct access to the DOM can make your application more vulnerable to XSS attacks.允许直接访问 DOM 会使您的应用程序更容易受到 XSS 攻击。 Carefully review any use of ElementRef in your code.仔细检查代码中对ElementRef任何使用。 For more detail, see the Security Guide .有关更多详细信息,请参阅安全指南

If you can avoid using ElementRef (without going direct to the DOM), that's probably best.如果您可以避免使用ElementRef (无需直接访问 DOM),那可能是最好的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM