Utilizing computed properties allows you to create functions that modify values into something different.
For instance, consider the following code snippet.
<div id="example">
{{ message.split('').reverse().join('') }}
</div>
You can introduce a computed property to execute JavaScript code and then call it within the HTML template
<div id="example">
<p>Original message: "{{ message }}"</p>
<p>Computed reversed message: "{{ reversedMessage }}"</p> //computed function
</div>
var vm = new Vue({
el: '#example',
data: {
message: 'Hello'
},
computed: {
// a computed getter
reversedMessage: function () {
// `this` refers to the vm instance
return this.message.split('').reverse().join('')
}
}
})
Further information can be found in the official documentation
Example showing usage with v-for
<div v-for="k in keys" :key="k">
{{ k }}
<div>
{{ reversedMessage(k) }}
</div>
</div>
var vm = new Vue({
el: '#example',
computed: {
// a computed getter
reversedMessage(k){ //take the value for each k in keys
return k.split('').reverse().join('')
}
}
})