问题
I'm trying to write a function, which is related to the DOM, but the problem is , polymer calls the function before the DOM is loaded, and as expected it gives me an undefined error!
I was looking for an event like onComplete or anything like this, so that It could read my function after loading the DOM.
*** the best example is ready function in Jquery
P.S : "ready:" event in polymer doesn't work properly,
回答1:
You could do something like this:
var domReady = function (func) {
if ('complete' === document.readyState) {
func();
return;
}
document.addEventListener('DOMContentLoaded', function () {
func && func();
});
};
domReady(function () {
// Your code
});
回答2:
In Polymer 2.0 you can use this into connectedCalback life cycle method:
connectedCallback(){
super.connectedCallback();
Polymer.RenderStatus.beforeNextRender(this, function() {
// All references to the dom elements into the web component
// [...]
});
}
来源:https://stackoverflow.com/questions/40769462/how-to-call-a-function-in-polymer-after-the-elements-have-been-created-like-rea