我有这个Vue.js代码:
new Vue({
data:{
myValue:'x',
myOtherValue:'y'
},
computed: {
myComputed: myFunction(){
return this['my' + 'Value']
}
}
})
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,计算属性将被缓存,并且仅依赖于它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']
}
}
})
Run Code Online (Sandbox Code Playgroud)
额外奖励:我将很感激我链接到VueJS代码中的相关功能:https://github.com/vuejs/vue