Vue3 computed 属性相互依赖导致栈溢出?如何解决?
时间:2024-11-21 15:37:01 313浏览 收藏
大家好,我们又见面了啊~本文《Vue3 computed 属性相互依赖导致栈溢出?如何解决?》的内容中将会涉及到等等。如果你正在学习文章相关知识,欢迎关注我,以后会给大家带来更多文章相关文章,希望我们能一起进步!下面就开始本文的正式内容~

vue3 computed 中的代码导致栈溢出?
问题:
一段 vue3 代码中使用 computed 导致了栈溢出,原因不明确。
// index.vue
<custom-calendar :check-date="checkdate" class="calendar-box" />
// customcalendar.vaue
<script lang="ts" setup>
const props = defineprops({
checkdate: {
type: array,
default() {
return [];
},
},
});
const mindate = computed(() => {
if (props.checkdate.length) {
let newarr = props.checkdate.sort((a, b): number => a.gettime() - b.gettime());
return new date(+newarr[0] as number);
} else {
return new date();
}
});
const maxdate = computed(() => {
if (props.checkdate.length) {
let newarr = props.checkdate.sort((a, b): number => b.gettime() - a.gettime());
return new date(+newarr[0] as number);
} else {
return new date();
}
});
const curyear = ref<number>(new date().getfullyear());
const curmonth = ref<number>(new date().getmonth());
watch(() => maxdate.value, (newval: date | null) => {
if (newval) {
curyear.value = newval.getfullyear();
curmonth.value = newval.getmonth();
}
}, {
immediate: true,
});
</script>答案:
该问题是由相互依赖的 computed 属性导致的无限循环引起的。
mindate 和 maxdate computed 属性都依赖于 props.checkdate 数组。如果 checkdate 数组发生变化,这两个属性都会重新计算。在重新计算过程中,它们又会对 checkdate 数组进行排序,这可能导致它们相互触发重新计算,形成无限循环。
以下是对代码的修改,以解决此问题:
import { ref, computed, watch, onMounted } from 'vue';
const props = defineProps({
checkDate: {
type: Array,
default() {
return [];
},
},
});
// 新增一个响应式属性来存储排序后的数组
const sortedCheckDates = ref([]);
const minDate = computed(() => {
if (sortedCheckDates.value.length) {
return new Date(sortedCheckDates.value[0].getTime());
} else {
return new Date();
}
});
const maxDate = computed(() => {
if (sortedCheckDates.value.length) {
return new Date(sortedCheckDates.value[sortedCheckDates.value.length - 1].getTime());
} else {
return new Date();
}
});
// 监听checkDate的变化,并更新sortedCheckDates
watch(() => props.checkDate, (newVal) => {
sortedCheckDates.value = newVal.sort((a, b) => a.getTime() - b.getTime());
});
const curYear = ref(new Date().getFullYear());
const curMonth = ref(new Date().getMonth());
watch(() => maxDate.value, (newVal) => {
if (newVal) {
curYear.value = newVal.getFullYear();
curMonth.value = newVal.getMonth();
}
}, {
immediate: true,
});
onMounted(() => {
// 初始化sortedCheckDates
sortedCheckDates.value = props.checkDate.sort((a, b) => a.getTime() - b.getTime());
});此修改通过引入一个额外的响应式属性 sortedcheckdates 来存储排序后的 checkdate 数组,解决了 computed 属性之间的相互依赖性。现在,当 checkdate 数组发生变化时,sortedcheckdates 数组将更新,并由 computed 属性使用它,从而避免了无限循环。
今天带大家了解了的相关知识,希望对你有所帮助;关于文章的技术知识我们会一点点深入介绍,欢迎大家关注golang学习网公众号,一起学习编程~
相关阅读
更多>
-
502 收藏
-
501 收藏
-
501 收藏
-
501 收藏
-
501 收藏
最新阅读
更多>
-
278 收藏
-
220 收藏
-
202 收藏
-
428 收藏
-
105 收藏
-
2. CSS样式设置在CSS中设置初始的滤镜效果,并定义过渡动画:.filter-image { width: 300px; height" class="aBlack">CSS实现图片滤镜过渡效果,可以通过transition属性配合filter来实现。以下是具体步骤和示例代码:1. 基本结构首先,在HTML中放置一张图片:
2. CSS样式设置在CSS中设置初始的滤镜效果,并定义过渡动画:.filter-image {
width: 300px;
height345 收藏 -
199 收藏
-
350 收藏
-
148 收藏
-
262 收藏
-
215 收藏
-
362 收藏
课程推荐
更多>
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 543次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 516次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 500次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 485次学习