This article mainly introduces the use of $watch in Vue.js. The editor thinks it is quite good. Now I will share it with you and give it as a reference. Let’s follow the editor and take a look.
I have learned a lot about $watch in Vue.js in the past two days, and it is very important, so I will add a few notes today.
github source code
Observer, Watcher, vm can be said to be the more important parts of Vue. After detecting data changes ViewUpdate is an important link. Let's take a look at how to implement a simple $watch function. Of course, Vue uses many optimization methods, which will not be discussed one by one in this article.
Example:
// 创建 vm let vm = new Vue({ data: 'a' }) // 键路径 vm.$watch('a.b.c', function () { // 做点什么 })
First explain their relationship in this demo and Vue:
After vm calls $watch, it first calls observe Function Create an Observer instance to observe data, and the Observer creates a Dep, which is used to maintain subscribers. Then create a Watcher instance to provide the update function. Once the data changes, callback functions are executed layer by layer.
Observer and observe
RecursiveCall the observe function to create an Observer. In the process of creating an Observer, use the Object.defineProperty() function to add the get set function to it and create a Dep instance.
export function observe (val) { if (!val || typeof val !== 'object') { return } return new Observer(val) }
function defineReactive (obj, key, val) { var dep = new Dep() var property = Object.getOwnPropertyDescriptor(obj, key) // 是否允许修改 if (property && property.configurable === false) { return } // 获取定义好的 get set 函数 var getter = property && property.get var setter = property && property.set var childOb = observe(val) Object.defineProperty(obj, key, { enumerable: true, configurable: true, get: () => { var value = getter ? getter.call(obj) : val // 说明是 Watcher 初始化时获取的, 就添加订阅者 if (Dep.target) { dep.depend() if (childOb) { childOb.dep.depend() } // if isArray do some.... } return value }, set: (newVal) => { var value = getter ? getter.call(obj) : val if (value === newVal) { return } if (setter) { setter.call(obj, newVal) } else { val = newVal } childOb = observe(newVal) dep.notify() } }) }
You may wonder what the hell is Dep.target?
The above is the detailed content of Detailed explanation of how to use watch in Vue.js. For more information, please follow other related articles on the PHP Chinese website!

你可能遇到过智能手机屏幕出现绿色线条的问题,即使没见过,也一定在网络上看到过相关图片。那么,智能手表屏幕变白的情况你遇见过吗?4月2日,CNMO从外媒了解到,一名Reddit用户在社交平台上分享了一张图片,展示了三星Watch系列智能手表屏幕变白的情况。该用户写道:"我离开时正在充电,回来时就这样了,我尝试重启,但重启过程中屏幕还是这样。"三星Watch智能手表屏幕变白这位Reddit用户并未指明这款智能手表的具体型号。不过,从图片上看,应该是三星Watch5。此前,另一位Reddit用户也报告

我们都知道监听器的作用是在每次响应式状态发生变化时触发,在组合式API中,我们可以使用watch()函数和watchEffect()函数,当你更改了响应式状态,它可能会同时触发Vue组件更新和侦听器回调。默认情况下,用户创建的侦听器回调,都会在Vue组件更新之前被调用。这意味着你在侦听器回调中访问的DOM将是被Vue更新之前的状态。那么,我们来看一下,怎么才能好好的使用他们呢?他们之间又有什么区别呢?watch()函数watch需要侦听特定的数据源,比如侦听一个ref,watch的第一个参数可以

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

如何在watchOS10中访问控制中心自从苹果推出第一款AppleWatch以来,我们与手表互动的方式或多或少保持不变。即使在添加了如此多的新功能之后,整体用户界面也保持一致。但是watchOS10带来了重大变化!在執行watchOS9或更低版本的AppleWatch上,您可以通過在螢幕上向上轻歚來快速開啟控制中心。但是,随着watchOS10的更新,向上滑动手势会拉起全新的小部件智能堆栈,而不是控制中心。所以最大的问题是如何在WatchOS10中打开AppleWatch上的控制中心。答案如下:

本篇文章带大家了解一下Vue3中的watchEffect,介绍一下它的副作用,并聊聊它可以做什么事情,希望对大家有所帮助!

Vue中如何使用watch监听数组的变化Vue是目前前端开发中使用较广泛的框架之一,它提供了许多便捷的方式来实现数据响应式、模板渲染以及组件化等功能。在Vue中,我们经常会使用watch来监听数据的变化,但是,当需要监听数组变化时,我们需要注意一些细节。在Vue中,我们可以使用watch来监听单个属性或者对象的变化,其基本使用方式如下:watch:{


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Mac version
God-level code editing software (SublimeText3)

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

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.

Zend Studio 13.0.1
Powerful PHP integrated development environment
