vue watch method code example
Example 1: vue watch deep
export default {
name: 'ColorChange',
props: {
colors: {
type: Array,
required: true,
},
},
watch: {
colors: {
deep: true,
handler(value) {
console.log('The list of colors has changed!', value);
}
}
}
}
Example 2: vue watch
var vm = new Vue({
el: '#demo',
data: {
firstName: 'Foo',
lastName: 'Bar',
fullName: 'Foo Bar'
},
watch: {
firstName: function (val) {
this.fullName = val + ' ' + this.lastName
},
lastName: function (val) {
this.fullName = this.firstName + ' ' + val
}
}
})
Example 3: vue computed
var vm = new Vue({
el: '#example',
data: {
message: 'Hello'
},
computed: {
reversedMessage: function () {
return this.message.split('').reverse().join('')
}
}
})
Example 4: vue watch
methods: {
reverseMessage: function () {
return this.message.split('').reverse().join('')
}
}