哪个先发生? WebComponentsReady 或 dom-改变?
Which one happens first? WebComponentsReady or dom-change?
我刚开始使用 Polymer。似乎有两个事件指示内容已准备就绪:
// Listen for template bound event to know when bindings
// have resolved and content has been stamped to the page
app.addEventListener('dom-change', function() {
console.log('Our app is ready to rock!');
});
// See https://github.com/Polymer/polymer/issues/1381
window.addEventListener('WebComponentsReady', function() {
// imports are loaded and elements have been registered
});
不知是否有必要把它们包起来,把代码放在里面,确保在执行任何脚本之前,文档已完全加载,例如:
app.addEventListener('dom-change', function() {
window.addEventListener('WebComponentsReady', function() {
// scripts go here
});
});
但是,我不知道在所有浏览器中这样做的正确方法是什么。如果 WebComponentsReady 发生在 dom-change 之前,则内部脚本永远不会执行。
哎呀,这甚至可能不是必需的,因为聚合物入门套件不会将它们包裹在一起。在那种情况下,哪些类型的脚本应该放在 dom-change
事件中,哪些类型的脚本应该放在 WebComponentsReady
事件中?
使用原生 ready
回调作为 described here。
<script>
(function() {
Polymer({
is: 'example-element',
properties: {...},
ready: function() {
// access a local DOM element by ID using this.$
this.$.header.textContent = 'Hello!';
}
});
})();
</script>
我刚开始使用 Polymer。似乎有两个事件指示内容已准备就绪:
// Listen for template bound event to know when bindings
// have resolved and content has been stamped to the page
app.addEventListener('dom-change', function() {
console.log('Our app is ready to rock!');
});
// See https://github.com/Polymer/polymer/issues/1381
window.addEventListener('WebComponentsReady', function() {
// imports are loaded and elements have been registered
});
不知是否有必要把它们包起来,把代码放在里面,确保在执行任何脚本之前,文档已完全加载,例如:
app.addEventListener('dom-change', function() {
window.addEventListener('WebComponentsReady', function() {
// scripts go here
});
});
但是,我不知道在所有浏览器中这样做的正确方法是什么。如果 WebComponentsReady 发生在 dom-change 之前,则内部脚本永远不会执行。
哎呀,这甚至可能不是必需的,因为聚合物入门套件不会将它们包裹在一起。在那种情况下,哪些类型的脚本应该放在 dom-change
事件中,哪些类型的脚本应该放在 WebComponentsReady
事件中?
使用原生 ready
回调作为 described here。
<script>
(function() {
Polymer({
is: 'example-element',
properties: {...},
ready: function() {
// access a local DOM element by ID using this.$
this.$.header.textContent = 'Hello!';
}
});
})();
</script>