admin管理员组文章数量:1419198
I am using VueScroll as a container for a carousel. Unfortunately I noticed that when a user has their mouse on the VueScroll element and wheel-scrolls the elements inside the VueScroll to zoom in/out. I am wanting to get the element and add a listener to handle the wheel scroll event.
To get the element I tried the following:
created(){
if(process.client){
if(document.getElementById('vueScroller')){
console.log('vueScroller created');
} else {
console.log('fail...')
}
}
},
And I noticed that on the initial load I get "fail..." but if I change something in the code and the app hotreloads I get "vueScroller created", so I figure I am close but not understanding where I am going wrong and/or why it acts differently depending on what triggers the page to load.
I am using VueScroll as a container for a carousel. Unfortunately I noticed that when a user has their mouse on the VueScroll element and wheel-scrolls the elements inside the VueScroll to zoom in/out. I am wanting to get the element and add a listener to handle the wheel scroll event.
To get the element I tried the following:
created(){
if(process.client){
if(document.getElementById('vueScroller')){
console.log('vueScroller created');
} else {
console.log('fail...')
}
}
},
And I noticed that on the initial load I get "fail..." but if I change something in the code and the app hotreloads I get "vueScroller created", so I figure I am close but not understanding where I am going wrong and/or why it acts differently depending on what triggers the page to load.
Share Improve this question edited Nov 16, 2019 at 23:22 halfer 20.4k19 gold badges109 silver badges202 bronze badges asked Nov 5, 2019 at 16:48 av0000av0000 1,9676 gold badges34 silver badges55 bronze badges 1- 2 Use a ref instead of a DOM lookup – Chase DeAnda Commented Nov 5, 2019 at 23:05
1 Answer
Reset to default 3If you use created()
the template has not yet been rendered. Using beforeMount()
or mounted()
should fix your problem.
Have a look at the life cycle diagram for reference: https://v2.vuejs/v2/guide/instance.html#Lifecycle-Diagram
版权声明:本文标题:javascript - VueNuxt - Getting element by id in client returns null on load but works on reload - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745304371a2652556.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论