Computed properties are typically dependent on other data properties. They behave exactly like the language standard get
/set
properties. Any change to the dependent properties will trigger the logic for the computed property.Computed properties are cached based on their dependencies so they'll only rerun if a dependency changes. (for ex; a computed property that returns a new Date()
will never return because the logic will never run more than 1 time).
<!DOCTYPE html>
<html lang="en">
<head>
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
<div id="app">
<div>Here Random number Coming from Method: {{ getRandomNumberByMethod() }}</div>
<div>Here Random number Coming from Computed: {{ getRandomNumberByComputed }}</div>
<div>Here Random number Coming from Computed: {{ getRandomNumberByComputed }}</div>
<div>Here Random number Coming from Method: {{ getRandomNumberByMethod() }}</div>
<div>Here Random number Coming from Method: {{ getRandomNumberByMethod() }}</div>
<div>Here Random number Coming from Computed: {{ getRandomNumberByComputed }}</div>
</div>
<script>
new Vue ({
el: '#app',
data:{
},
methods: {
getRandomNumberByMethod:function() {
return Math.random();
},
},computed: {
getRandomNumberByComputed:function() {
return Math.random();
},
}
})
</script>
</body>
</html>