简体   繁体   English

聚合物1.0 dom-if vs hidden

[英]Polymer 1.0 dom-if vs hidden

From https://www.polymer-project.org/1.0/docs/api/dom-if 来自https://www.polymer-project.org/1.0/docs/api/dom-if

When if becomes falsey, the stamped content is hidden but not removed from dom. 如果if变为falsey,则标记的内容将被隐藏但不会从dom中删除。 When if subsequently becomes truthy again, the content is simply re-shown. 如果随后再次变得真实,则简单地重新显示内容。 This approach is used due to its favorable performance characteristics: the expense of creating template content is paid only once and lazily. 由于其有利的性能特征,因此使用此方法:创建模板内容的费用仅支付一次且懒惰。

I thought this was the behavior of hidden attribute, thus hidden being cheaper than dom-if since the template would not get restamped with hidden . 我认为这是hidden属性的行为,因此hiddendom-if便宜dom-if因为模板不会被hidden重新放大。 Since 'no restamp' is the default behavior dom-if , what is the difference between dom-if and hidden and how is hidden better for performance? 由于“没有restamp”是默认行为dom-ifdom-ifhidden之间的区别是什么以及如何更好地hidden性能? Polymer best practices notes that hidden is cheaper. 聚合物最佳实践指出hidden的更便宜。

My understanding of it is that dom-if does not stamp until the expression becomes truthy, but after doing so it behaves much like [hidden] does. 我对它的理解是dom-if在表达式变得真实之前不会加盖,但是在这样做之后它的表现就像[hidden]那样。 In that way dom-if is a pessimistic [hidden] that defers stamping for as long as possible. 就这样, dom-if是一种悲观的[hidden] ,它会尽可能地延迟冲压。

This lazy-loading approach is favorable in certain situations where stamping the template would be very resource intensive. 这种延迟加载方法在某些情况下是有利的,其中标记模板将是非常耗费资源的。 For example if the template was very large with multiple custom components that have to be initialized. 例如,如果模板非常大,需要初始化多个自定义组件。 If you had just used the hidden attribute there you would pay the performance cost of creating all of that DOM only for it to not be visible until later. 如果您刚刚使用了hidden属性,那么您只需支付创建所有DOM的性能成本,直到稍后才能看到它。

For simple cases, such as hiding or showing some text, a div or two, etc. the hidden attribute may be faster because the cost of creating those elements and then hiding them may be less than creating a <template> instance to hold your code, Polymer setting up listeners to monitor the expression for truthiness, then, when it becomes truthy, all the overhead for stamping the template, parsing it for binding expressions, etc. This is particularly true for the browsers where <template> support is polyfilled. 对于简单的情况,例如隐藏或显示一些文本,div或两个等等, hidden属性可能更快,因为创建这些元素然后隐藏它们的成本可能少于创建<template>实例来保存代码,Polymer设置监听器以监控表达式的真实性,然后,当它变得真实时,标记模板的所有开销,解析绑定表达式等等。对于<template>支持被填充的浏览器尤其如此。

Thinking it through for your situation (and ideally testing it) is the best route to take. 仔细考虑你的情况(并且理想地测试它)是最好的选择。 Often times the differences may be negligible but do take special care if this part of your code happens to be in a dom-repeat with a lot of items or anything that happens frequently. 通常情况下,这些差异可以忽略不计,但如果您的代码中的这部分恰好与大量项目或经常发生的任何事情发生dom-repeat ,则需要特别小心。 The difference could add up. 差异可能会加起来。

Hidden interferes with the display CSS property and therefore dom-if is a better option. 隐藏会干扰display CSS属性,因此dom-if是更好的选择。

If you have 如果你有

:host {display: block;}

setting hidden on the components host element won't hide it. hidden在组件主机元素上的设置不会隐藏它。 You would also need to add a global style like 您还需要添加一个全局样式

[hidden] { display: none !important;}

to make it work reliably (and to make it work in IE9 which doesn't support hidden AFAIR). 使其工作可靠(并使其在IE9中工作,不支持hidden AFAIR)。

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

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