javascript - Polymer 和 WebComponentsReady 事件

标签 javascript html polymer web-component

根据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/

相关文章:

javascript - 如何顺序执行javascript?

javascript - jQuery 和 Twitter Bootstrap - 下拉列表拆分

html - 无法在 IE 中设置样式复选框

polymer - paper-item - 为 paper-dropdown 赋予值(value)的方法?

javascript - $mdDialog 使用函数关闭

javascript - 在提交该页面之前检查表单字段值

html - ul 没有得到正确的高度

html - CSS 表 : bottom borders that don't touch and rounded corners

javascript - 处理 polymer 元素继承的最佳方法

css - Polymer : Alternate color depending on user role (admin, 普通用户,特殊用户)