【发布时间】:2017-01-09 07:30:57
【问题描述】:
我有这个 Vue.js 代码:
new Vue({
data:{
myValue:'x',
myOtherValue:'y'
},
computed: {
myComputed: myFunction(){
return this['my' + 'Value']
}
}
})
如您所见,计算的属性将被缓存,并且仅依赖于data.myValue。我的问题是 Vue.js 缓存系统如何知道只有在 myValue 更改时才再次运行计算函数?
如果我更改myOtherValue 变量,myComputed 函数将使用缓存,我不会再次调用它。
我想了几种方法来实现它。但是 Vuejs 是怎么做到的呢? 我已阅读这篇文章:https://vuejs.org/v2/guide/computed.html 并没有找到答案。
这段代码中发生了什么,它将依赖什么?
const flag=2
new Vue({
data:{
myValue:'x',
myOtherValue:'y'
},
computed: {
myComputed: myFunction(){
if (flag==1){
return this['my' + 'Value']
}
else
return this['my' + 'Other' + 'Value']
}
}
})
奖励:我将不胜感激我在 VueJS 代码中链接到相关函数:https://github.com/vuejs/vue
【问题讨论】:
-
你应该阅读这个:docs.google.com/presentation/d/…也许它可以帮助你。
-
谢谢。我读过它。缓存系统就不解释了。
标签: javascript vue.js vuejs2