vue3 watch深度监听对象
时间: 2023-09-18 15:09:49 浏览: 212
vue watch关于对象内的属性监听
5星 · 资源好评率100%
在Vue 3中,使用watch函数进行深度监听对象的方法如下:首先,在setup函数内部创建一个ref对象,并将其作为监听对象传入watch函数。然后,在监听回调函数内部对新旧值进行处理。最后,在第三个参数对象中设置deep属性为true,表示进行深度监听。例如,可以使用以下代码进行深度监听对象:
```javascript
import { ref, watch } from 'vue';
const obj = ref({ name:'clt', age:'18' });
watch(obj, (newVal, oldVal) => {
console.log(newVal, oldVal);
}, { deep: true });
```
在这个例子中,obj是要进行深度监听的对象,监听回调函数接收新值newVal和旧值oldVal作为参数,并在控制台输出。通过将deep属性设置为true,可以实现对obj对象的深度监听。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *2* [Vue3的watch监听的多种情况](https://blog.csdn.net/m0_57482322/article/details/123157229)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"]
- *3* [vue3基础(五)watch(浅监听及深度监听),鼠标及键盘修饰符,v-model,对象写法,class使用数组,字符串...](https://blog.csdn.net/c62387723sq/article/details/126006732)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
阅读全文