javascript 在 Vue.js 中,如何禁用计算属性的缓存?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32106193/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
In Vue.js, how to disable caching of computed properties?
提问by Dave
Before 0.12.8, computed properties behave just like getters - every time you access it, the getter function is re-evaluated. In 0.12.8 this has been improved - computed properties are cached and lazily re-evaluated only when necessary.
在 0.12.8 之前,计算属性的行为就像 getter - 每次访问它时,都会重新评估 getter 函数。在 0.12.8 中,这已得到改进 - 仅在必要时缓存和延迟重新评估计算属性。
For my current project, I actually need some properties to be re-evaluated on every access. The reason the current lazy evaluation isn't working is because in some of my properties there are other "dynamic variables" that are not under Vue.js's watch.
对于我当前的项目,我实际上需要在每次访问时重新评估一些属性。当前惰性求值不起作用的原因是,在我的某些属性中,还有其他不受 Vue.js 监控的“动态变量”。
回答by Xethron
According to the docs, you can simply set cache to false:
根据文档,您可以简单地将缓存设置为 false:
computed: {
example: {
cache: false,
get: function () {
return Date.now() + this.msg
}
}
}
回答by Илья Зеленько
You can use way from @Xethron but using the short syntax of functions:
您可以使用来自@Xethron 的方式,但使用函数的简短语法:
computed: {
example: {
cache: false,
get () {
return Date.now() + this.msg
}
}
}