I am trying to get DOM element inside of .vue component, but i can`t bacause, as i think, the element have not rendered yet.
If i do something like this, it works:
setTimeout(() => {
$('.element').do_something();
}, 100);
But this is bad way.
I also tried to do something like this:
// app.js
var app = new Vue({
el: '#root',
methods: {
getElement(selector, cb()){
var el = $(selector)
if(el.length){
// I SEE THIS ELEMENT HERE
cb(el);
}else{
setTimeout(() => {
this.getElement(selector);
}, 10);
}
};
}
});
// ExampleComponent.vue
export default {
created() {
var el = app.getElement('.selector');
console.log(el);
}
}
But i got an error here:
Uncaught TypeError: cb is not a function
I also tried to use Promise instead of simple callback.
// app.js
var app = new Vue({
el: '#root',
methods: {
getElement(selector, cb()){
var el = $(selector);
if(el.length){
return new Promise((resolve) => {
// I SEE THIS ELEMENT HERE
console.log(el);
resolve(el);
});
}else{
setTimeout(() => {
this.getElement(selector);
}, 10);
}
};
}
});
// ExampleComponent.vue
export default {
created() {
app.getElement('.selector').then((el) => {
console.log(el)
});
}
}
But getElement return undefined and i have an error:
Uncaught (in promise) TypeError: Cannot read property 'then' of undefined
Any suggestions how i can access element?