Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to use direct DOM in Vuejs 3?

I want to use DOM in a Vuejs 3 project. How can I do that? I want to select element like querySelector in Vuejs 3. I have tried but it’s not working well.

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Vuejs uses virtual DOM, means a DOM specially made for vue. The virtual DOM is smaller in size than actual DOM, and it’s very efficient.

If you want to access a DOM element, vue has a solution, you can use Template Refs for that. ref is a special attribute, It allows us to obtain a direct reference to a specific DOM element or child component instance after it’s mounted.

According the official documentation, you can do something like this,

import { ref, onMounted } from 'vue'

// declare a ref to hold the element reference
// the name must match template ref value
const input = ref(null)

onMounted(() => {
  input.value.focus()
})
</script>

<template>
  <input ref="input" />
</template>

Furthermore, as vue is a JavaScript framework, you can use every funtionalities that JavaScript has, and as you have mentioned, your Vuejs 3 project should work with actual DOM functioinalities as well.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading