【发布时间】:2019-08-28 12:27:40
【问题描述】:
我有一个带有计算属性的自定义对象...
class MyObject {
get someComputedProp() {
// expensive computation based on some other props
}
}
以及一个包含约 500 个此类对象的 vuetify 数据表
<v-data-table
:headers="headers"
:items="myObjects"
:search="search"
>
<template slot="items" slot-scope="{ item }">
<td>{{ item.someComputedProp }}</td>
...
数据表加载速度很慢。实验时,我发现我昂贵的 getter 在整个表中每个对象被调用了大约 4 次。如果我用返回字符串文字替换昂贵的 getter 的代码,我的表很快。这带来了一些问题:
- 为什么每行调用 getter 这么多次?
- 表格有分页,即使我的 getter 必须每行调用 4 次,为什么必须为每一行调用它,即使是那些不在当前页面上的?
-
我可以让我的对象缓存昂贵的计算...
get someComputedProp() { if (!this._cachedComputedProp) this._cachedComputedProp = // expensive computation based on some other props } return this._cachedComputedProp }这将使 4 个调用中的 3 个调用便宜,但在另一个 vue 上,我需要计算的 prop 实时更新,因为它依赖的 props 已更新。现在我被困在做这种愚蠢的事情......
set propThatComputedPropDependsOn (value) { this._cachedComputedProp = null this._propThatComputedPropDependsOn = value }- 我该如何摆脱这个烂摊子?
【问题讨论】:
标签: javascript vue.js vuetify.js