在 dom-if 中使用监听器进行点击

Using a listener for a tap inside a dom-if

我有一个简单的按钮,我想在按下时触发一些东西。我给按钮一个 id 并为 id.tap 创建了一个监听器。这工作正常,但是当我将我的按钮放在 template[is=dom-if] 中时它停止工作。这是要像这样工作吗?我该如何解决?

dom-if 中的元素在创建元素时尚不存在,因此无法使用 this.$ 访问它们。给元素一个 on-tap 属性,或者使用 Polymer.dom(this.root).querySelector 来查找元素。

仅供参考,文档建议不要随意使用 dom-if

Since it is generally much faster to hide and show elements rather than destroy and recreate them, conditional templates are only useful to save initial creation cost when the elements being stamped are relatively heavyweight and the conditional may rarely (or never) be true in given usages. Otherwise, liberal use of conditional templates can actually add significant runtime performance overhead.

使用hidden$=condition 可能是最好的解决方案。