search
HomeWeb Front-endFront-end Q&AWhat does vue dom mean?
What does vue dom mean?Dec 20, 2022 pm 08:41 PM
vuedomvirtual dom

DOM is a document object model and an interface for HTML programming. Elements in the page are manipulated through DOM. The DOM is an in-memory object representation of an HTML document, and it provides a way to interact with web pages using JavaScript. The DOM is a hierarchy (or tree) of nodes with the document node as the root.

What does vue dom mean?

The operating environment of this tutorial: windows7 system, vue3 version, DELL G3 computer.

What is dom

dom is a document object model and an interface for HTML programming. The page is operated through dom Elements. When an HTML page is loaded, the browser creates a DOM, which provides a new logical structure to the document and can change the content and structure.

DOM is called the Document Object Model (DOM for short), which is a standard programming interface recommended by the W3C organization for processing extensible markup languages.

DOM is an in-memory object representation of an HTML document. It provides a way to interact with web pages using JavaScript. The DOM is a hierarchy (or tree) of nodes with the document node as the root.

In fact, DOM is a document model described in an object-oriented way. The DOM defines the objects required to represent and modify a document, the behaviors and properties of these objects, and the relationships between these objects.

Through JavaScript, we can reconstruct the entire HTML document. Such as adding, removing, changing or rearranging items on the page.

To change something on the page, JavaScript needs to gain access to all elements in the HTML document. This entry, along with the methods and properties for adding, moving, changing, or removing HTML elements, is obtained through the Document Object Model.

What is virtual DOM

I believe everyone is familiar with the concept of virtual DOM. From React to Vue, virtual DOM is Both frameworks bring cross-platform capabilities (React-Native and Weex)

In fact, it is just a layer of abstraction of the real DOM, with JavaScript objects (VNode nodes) as the base tree, using The attributes of the object are used to describe the nodes, and finally the tree can be mapped to the real environment through a series of operations

In the Javascript object, the virtual DOM is represented as an Object object. And it contains at least three attributes: tag name (tag), attributes (attrs) and child element objects (children). Different frameworks may have different names for these three attributes.

Creating a virtual DOM is just for better Render virtual nodes into the page view, so the nodes of the virtual DOM object correspond to the properties of the real DOM one by one.

Virtual DOM technology is also used in vue

Define the real DOM

<div id="app">
    <p class="p">节点内容</p>
    <h3 id="nbsp-foo-nbsp">{{ foo }}</h3>
</div>

Instantiate vue

const app = new Vue({
    el:"#app",
    data:{
        foo:"foo"
    }
})

Observe the render of render, we can get the virtual DOM

(function anonymous(
) {
	with(this){return _c(&#39;div&#39;,{attrs:{"id":"app"}},[_c(&#39;p&#39;,{staticClass:"p"},
					  [_v("节点内容")]),_v(" "),_c(&#39;h3&#39;,[_v(_s(foo))])])}})

Through VNode, vue can create nodes, delete nodes and modify the abstract tree For node operations, some minimum units that need to be modified are obtained through the diff algorithm, and then the view is updated, which reduces DOM operations and improves performance.

Several methods for Vue to obtain DOM

Although Vue implements the MVVM model and separates data and performance, we only need to update The data can update the DOM synchronously, but in some cases, you still need to obtain DOM elements for operation (for example, a library introduced requires passing in a root dom element as the root node, or writing some custom instructions). This article mainly introduces Several ways to get DOM elements in Vue.

Use the DOM API to find elements directly

<script>
	...
	mounted () {
		let elm = this.$el.querySelector(&#39;#id&#39;)
	}
</script>

This method is simple and intuitive enough. The Vue component will This.$el is assigned the value of the mounted root dom element. We can directly use the $el's querySelector, querySelectorAll and other methods to obtain matching elements.

refs

<template>
	<div ref="bar">{{ foo }}</div>
	<MyAvatar ref="avatar" />
	...
</template>
<script>
	...
	mounted () {
		let foo = this.$refs[&#39;bar&#39;] // 一个dom元素
		let avatar = this.$refs[&#39;avatar&#39;] // 一个组件实例对象
	}
</script>

Use the $refs of the component instance to get the componentrefThe element corresponding to the attribute.
If the ref attribute is added to a component, then what you get is the instance of this component, otherwise what you get is the dom element.

It is worth noting that when the v-for loop template directive is included, the ref attribute on the loop element and sub-element corresponds to an array (even if it is dynamically generated ref, also an array):

<template>
	<div v-for="item in qlist" :key="item.id" ref="qitem">
		<h3 id="nbsp-item-title-nbsp-nbsp">{{ item.title  }}</h3>
		<p ref="pinitem">{{ item.desc }}</p>
		<p :ref="&#39;contact&#39;+item.id">{{ item.contact }}</p>
	</div>
	...
</template>
<script>
	...
	data () {
		return {
			qlist: [
				{ id: 10032, title: &#39;abc&#39;, desc: &#39;aadfdcc&#39;, contact: 123 },
				{ id: 11031, title: &#39;def&#39;, desc: &#39;--*--&#39;, contact: 856 },
				{ id: 20332, title: &#39;ghi&#39;, desc: &#39;?/>,<{]&#39;, contact: 900 }
			]
		}
	},
	mounted () {
		let foo = this.$refs[&#39;qitem&#39;] // 一个包含dom元素的数组
		let ps = this.$refs[&#39;pinitem&#39;] // p元素是v-for的子元素,同样是一个数组
		let contact1 = this.$refs[&#39;contact&#39; + this.qlist[0].id] // 还是个数组
	}
</script>

The reason for this can be obtained from Vue’s part of the code on ref processing:

function registerRef (vnode, isRemoval) {
  var key = vnode.data.ref;
  if (!isDef(key)) { return }

  var vm = vnode.context;
  // vnode如果有componentInstance表明是一个组件vnode,它的componentInstance属性是其真实的根元素vm
  // vnode如果没有componentInstance则不是组件vnode,是实际元素vnode,直接取其根元素
  var ref = vnode.componentInstance || vnode.elm;
  var refs = vm.$refs;
  if (isRemoval) {
    ...
  } else {
  	// refInFor是模板编译阶段生成的,它是一个布尔值,为true表明此vnode在v-for中
    if (vnode.data.refInFor) {
      if (!Array.isArray(refs[key])) {
        refs[key] = [ref]; // 就算元素唯一,也会被处理成数组
      } else if (refs[key].indexOf(ref) < 0) {
        // $flow-disable-line
        refs[key].push(ref);
      }
    } else {
      refs[key] = ref;
    }
  }
}

Use custom instructions

Vue provides custom instructions. The official documentation gives the following usage methods, where el is the reference to the dom element

Vue.directive(&#39;focus&#39;, {
  // 当被绑定的元素插入到 DOM 中时……
  inserted: function (el) {
    // 聚焦元素
    el.focus()
  }
})

// 在模板中
<template>
	<input v-model="name" v-focus />
</template>

Regarding custom instructions, It is very useful in scenarios such as component libraries and event reporting.

[Related recommendations: vuejs video tutorial, web front-end development]

The above is the detailed content of What does vue dom mean?. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Vue常见面试题汇总(附答案解析)Vue常见面试题汇总(附答案解析)Apr 08, 2021 pm 07:54 PM

本篇文章给大家分享一些Vue面试题(附答案解析)。有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助。

5 款适合国内使用的 Vue 移动端 UI 组件库5 款适合国内使用的 Vue 移动端 UI 组件库May 05, 2022 pm 09:11 PM

本篇文章给大家分享5 款适合国内使用的 Vue 移动端 UI 组件库,希望对大家有所帮助!

vue中props可以传递函数吗vue中props可以传递函数吗Jun 16, 2022 am 10:39 AM

vue中props可以传递函数;vue中可以将字符串、数组、数字和对象作为props传递,props主要用于组件的传值,目的为了接收外面传过来的数据,语法为“export default {methods: {myFunction() {// ...}}};”。

手把手带你利用vue3.x绘制流程图手把手带你利用vue3.x绘制流程图Jun 08, 2022 am 11:57 AM

利用vue3.x怎么绘制流程图?下面本篇文章给大家分享基于 vue3.x 的流程图绘制方法,希望对大家有所帮助!

聊聊vue指令中的修饰符,常用事件修饰符总结聊聊vue指令中的修饰符,常用事件修饰符总结May 09, 2022 am 11:07 AM

本篇文章带大家聊聊vue指令中的修饰符,对比一下vue中的指令修饰符和dom事件中的event对象,介绍一下常用的事件修饰符,希望对大家有所帮助!

如何覆盖组件库样式?React和Vue项目的解决方法浅析如何覆盖组件库样式?React和Vue项目的解决方法浅析May 16, 2022 am 11:15 AM

如何覆盖组件库样式?下面本篇文章给大家介绍一下React和Vue项目中优雅地覆盖组件库样式的方法,希望对大家有所帮助!

通过9个Vue3 组件库,看看聊前端的流行趋势!通过9个Vue3 组件库,看看聊前端的流行趋势!May 07, 2022 am 11:31 AM

本篇文章给大家分享9个开源的 Vue3 组件库,通过它们聊聊发现的前端的流行趋势,希望对大家有所帮助!

react与vue的虚拟dom有什么区别react与vue的虚拟dom有什么区别Apr 22, 2022 am 11:11 AM

react与vue的虚拟dom没有区别;react和vue的虚拟dom都是用js对象来模拟真实DOM,用虚拟DOM的diff来最小化更新真实DOM,可以减小不必要的性能损耗,按颗粒度分为不同的类型比较同层级dom节点,进行增、删、移的操作。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft