Skip to Content
学习 Vue
book

学习 Vue

by Maya Shavin
May 2025
Intermediate to advanced
350 pages
4h 33m
Chinese
O'Reilly Media, Inc.
Content preview from 学习 Vue

第 2 章 Vue 如何工作:基础知识

本作品已使用人工智能进行翻译。欢迎您提供反馈和意见:translation-feedback@oreilly.com

在上一章中,您学习了构建 Vue 应用程序的基本工具,还创建了第一个 Vue 应用程序,为下一步做好了准备:通过编写 Vue 代码来学习 Vue 如何工作。

本章将向您介绍虚拟文档对象模型(Virtual DOM)的概念以及使用 Vue Options API 编写 Vue 组件的基础知识。本章还将进一步探讨 Vue 指令和 Vue 反应机制。在本章结束时,您将了解 Vue 的工作原理,并能够编写和注册一个 Vue 组件,供您的应用程序使用。

引擎盖下的虚拟 DOM

Vue 并不直接使用文档对象模型(DOM)。相反,它通过实现虚拟 DOM 来优化应用程序在运行时的性能 。

为了牢固理解虚拟 DOM 的工作原理,我们先从 DOM 的概念开始。

DOM 以内存树状数据结构的形式(如图 2-1 所示)表示网页上的 HTML(或 XML)文档内容。它是连接网页和实际编程代码(如 JavaScript)的编程接口。HTML 文档中的标签(如<div><section> )被表示为编程节点和对象 。

An image illustrated different HTML elements connected, distributing by nesting levels
图 2-1. DOM 树示例

浏览器解析 HTML 文档后,DOM 将立即可供交互使用。在布局发生变化时,浏览器会在后台不断绘制和重新绘制 DOM。我们将解析和绘制 DOM 的过程称为屏幕光栅化或像素到屏幕流水线。图 2-2演示了光栅化的工作原理 :

An image illustrated a flow diagram consisting of five major steps, including parsing HTML and CSS code, calculating the CSS styles for elements, planning for screen layout, then painting the visual elements, and finally applying the composition layer on them on the browsers. It also highlights where repaint and reflow happens whenever layout changes happen.
图 2-2. 浏览器光栅化流程

版面更新问题

每次刷新都会对浏览器的性能造成很大影响。由于 DOM 可能由许多节点组成,因此查询和更新单个或多个节点的成本会非常, 。

下面是 DOM 中li 元素列表的一个简单示例:

<ul class="list" id="todo-list">
  <li class="list-item">To do item 1</li>
  <li class="list-item">To do item 2</li>
  <!--so on…-->
</ul>

添加/删除li 元素或修改其内容需要使用document.getElementById (或document.getElementsByClassName )查询该项目的 DOM。然后,您需要使用相应的 DOM API 执行所需的更新。

例如,如果您想在前面的示例中添加一个新项目,您需要执行以下步骤:

  1. 通过id 属性的值查询包含的列表元素"todo-list"

  2. 使用li 添加新元素document.createElement()

  3. 使用setAttribute() 设置textContent 和相关属性,以符合其他元素的标准。

  4. 使用appendChild() 将该元素作为子元素追加到步骤 1 中找到的列表元素中:

const list = document.getElementById('todo-list');

const newItem = document ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

学习 React,第二版

学习 React,第二版

Alex Banks, Eve Porcello
学习 Java,第 6 版

学习 Java,第 6 版

Marc Loy, Patrick Niemeyer, Daniel Leuck
Docker:入门与实践,第三版

Docker:入门与实践,第三版

Sean P. Kane, Karl Matthias

Publisher Resources

ISBN: 9798341657786