I read the official document says:
The difference betweencomputed
and methods
is that computed
will be cached based on their dependencies. If the data cannot be changed, computed
It will not be re-executed when refreshing, but methods
will be executed every time.
But the examples I wrote are not like this (the examples I wrote are official examples).
html:
<p id="app">
<p>{{methodsNow()}}</p>
<p>{{computedNow}}</p>
</p>
javascript:
new Vue({
el:'#app',
data:{
},
methods:{
methodsNow:function(){
return new Date().toLocaleString();
}
},
computed:{
computedNow:function(){
return new Date().toLocaleString();
}
}
});
Let’s discuss, did I write something wrong somewhere?
你的例子对于描述二者不同这点上,没有帮助。给你看这个例子:JSFiddle
你这样测试下就明白了
html:
javascript:
结果你发现computedNow执行了一次,methodsNow执行了两次