how to call a function in polymer after the elements have been created (like ready function in jquery)

為{幸葍}努か 提交于 2019-12-24 07:56:45

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!