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?
Your example is not helpful in describing the difference between the two. Show you this example: JSFiddle
You will understand after testing it like this
html:
javascript:
As a result, you find that computedNow is executed once and methodsNow is executed twice