vue3 | shallowReactive 、shallowRef、triggerRef

shallowReactive

使用 reactive 声明的变量为递归监听,使用 shallowReactive 声明的变量为非递归监听(通俗的讲就是 reactive 创建的对象将会被 vue 内部进行递归监听,可以监听到对象中的对象是否发生了改变从而更新视图,而 shallowReactive 创建的对象只能监听到首层对象的变化)。

<script setup lang="ts"> import { shallowReactive } from 'vue' const state = shallowReactive({ a: 1, b: { res: 2, c: { res: 3, }, }, }) const handleCLick = () => { state.a = 100 state.b.res = 200 state.b.c.res = 300 } </script>
<template> <n-el class="flex flex-col justify-center items-center w-full h-200"> <n-el>{{ state.a }}</n-el> <n-el>{{ state.b.res }}</n-el> <n-el>{{ state.b.c.res }}</n-el> <n-button @click="handleCLick" class="!w-20">点击</n-button> </n-el> </template>

shallowRef

其中 shallowRef 为非递归监听,ref 为递归监听,与 shallowReactive 和 reactive 不同的是 shallowRef 和 ref 监听的对象首层是 value 这一层,只有当 value 发生改变时 shallowRef 声明的变量才会在视图上进行更新。
shallowRef只有对  .value  的访问是响应式的。

<n-el>{{ state2.res }}</n-el> <n-button class="!w-20" @click="handleCLick">点击</n-button> ...... const handleCLick = () => { // state2.value.res = 9999 //不会触发 }

const handleCLick = () => { //会触发 state2.value = { res: 9999, }; };

<n-el>{{ state2.res }}</n-el> <n-el>{{ state2.res2.data }}</n-el> <n-el>{{ state2.res2.res3.data }}</n-el> <n-button class="!w-20" @click="handleCLick">点击</n-button> ...... const handleCLick = () => { state2.value = { res: 100, res2: { data: 200, res3: { data: 300, }, }, } }

triggerRef

triggerRef 的作用则是手动执行与 shallowRef 关联的任何副作用,强制更新视图。

const handleCLick = () => { state.value.a = 100 state.value.b.res = 200 state.value.b.c.res = 300 state2.value.res = 9999 triggerRef(state2) } ...... <n-el>{{ state2.res }}</n-el> <n-el>{{ state2.res2.data }}</n-el> <n-el>{{ state2.res2.res3.data }}</n-el> <n-button class="!w-20" @click="handleCLick">点击</n-button>

参考文档:

1、https://www.jianshu.com/p/4e0d4fcff950

2、https://blog.csdn.net/zxBlogs/article/details/114546382


__EOF__

本文作者杨芋可可
本文链接https://www.cnblogs.com/yangyukeke/p/17099696.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   杨芋可可  阅读(97)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
点击右上角即可分享
微信分享提示