gpt4 book ai didi

vue.js - 每次更新值时都会运行计算值

转载 作者:行者123 更新时间:2023-12-03 06:47:33 25 4
gpt4 key购买 nike

我有一个输出复选框的循环:

<div v-for="(value, key) in optionsObject" :key="key">
在计算中,我检查 Prop 是否 options是一个对象或一个数组(如果数组我从数组值中创建它们的键)...
computed: {
optionsObject() {
return _.isPlainObject(this.options) ? this.options : _.zipObject(this.options, this.options);
}
}
但是每次更改复选框时,它都会运行计算的 optionsObject再次。有没有办法防止这种情况?

最佳答案

这就是 Vue 中计算属性的工作方式。当它所依赖的属性发生变化时它会刷新。正如 docs 中所述,对于性能而言,与 method() 相比,这是一个巨大的优势。 :

Instead of a computed property, we can define the same function as amethod. For the end result, the two approaches are indeed exactly thesame. However, the difference is that computed properties are cachedbased on their reactive dependencies. A computed property will onlyre-evaluate when some of its reactive dependencies have changed.

关于vue.js - 每次更新值时都会运行计算值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62862204/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com