根据Polymer docs , WebComponentsReady
事件是必需的,因为...
The polyfills parse element definitions and handle their upgrade asynchronously. If you prematurely fetch the element from the DOM before it has a chance to upgrade, you’ll be working with an HTMLUnknownElement. In these situations, wait for the WebComponentsReady event before interacting with the element
我有一个 HTML 页面,该页面导入单个 Web 组件并注册一个处理程序,该处理程序在加载所有 Web 组件时记录一条语句:
<!DOCTYPE html>
<html>
<head>
<script src="bower_components/platform/platform.js"></script>
<link rel="import" href="elements/my-element.html">
</head>
<body unresolved>
<my-element></my-element>
<script>
window.addEventListener('WebComponentsReady', function(e) {
console.log('components ready');
});
</script>
</body>
</html>
为什么 WebComponentsReady
事件在我的元素的 ready
polymer 事件之前触发?我需要知道什么时候可以与自定义元素交互,例如更改其属性并调用其公共(public)方法。
最佳答案
在 Polymer 1.0 中,您可以只监听 WebComponentsReady
。
在 Polymer 0.5 中,因为它异步执行更多操作,所以有一个名为 polymer-ready
的额外事件,它会在您的元素加载时触发。这是 a jsbin showing the order .
关于javascript - Polymer 和 WebComponentsReady 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21763690/