问题
I need to know when I can begin using my custom Polymer elements programmatically. The elements are still undefined
even in my window.onload
handler. Is there an established way of doing this correctly with Polymer 1.0?
-Edit-
I see there is a downvote, so let me clarify the issue. I have the following custom element definition:
<link rel="import" href="../../bower_components/polymer/polymer.html">
<dom-module id="element-one">
<style>
:host { display: block; background-color: blue; }
</style>
<template>
<h1>Element One</h1>
<content></content>
</template>
</dom-module>
<script>
var ElementOne = Polymer({
is: "element-one"
});
</script>
I then import this in my index.html
:
<link rel="import" href="elements/element-one/element-one.html">
And at the bottom of index.html
, before the </body>
tag, I try to instantiate a ElementOne
element:
<script>
console.log(typeof ElementOne); // undefined
var el = new ElementOne(); // fails, obviously
// try on window load
window.onload = function () {
console.log(typeof ElementOne); // undefined
};
</script>
I should note that this issue occurs in the latest Firefox and IE 10/11, but not in Chrome.
回答1:
use WebComponentsReady event:
window.addEventListener('WebComponentsReady', function(e) {
...
});
来源:https://stackoverflow.com/questions/30667685/is-there-an-equivelent-to-the-polymer-ready-event-in-polymer-1-0