It is a no-go to manipulate the DOM in Vue.js directly:
Instead, you want to make use of refs:
ref
is a special attribute that allows us to obtain a direct reference to a specific DOM element or child component instance after it's mounted.
Warning
Note that you can only access the ref after the component is mounted because the element doesn't exist until after the first render.
Let's take a look at the above example using a template ref:
If you are trying to watch for changes of a template ref, make sure to check if the ref has a null value:
If you liked this Vue tip, follow me on X to get notified about new tips, blog posts, and more. Alternatively (or additionally), you can subscribe to my weekly Vue & Nuxt newsletter:
Vue Tip: Use Vue App Instance as Global Store
Vue provides an object that can be used to register global properties that can be accessed on any component instance inside the application.
Vue Tip: Creating a Custom Directive
In addition to the default set of directives like v-model or v-show, you can also register your own custom directives.