Event Based Prop Update Vue JS
by asemahle
HTML
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.0.7/vue.js"></script>
<div id="app" v-cloak>
{{title}}
<custom-component :val="title" v-on:val-changed="title = arguments[0]"></custom-component>
</div>
<template id="custom-component">
<button v-on:click="change">CHANGE!</button>
</template>
JavaScript
Vue.component('custom-component', {
template: '#custom-component',
props: {
val: Object,
},
data: function() {
return {
times: 0
}
},
methods: {
change: function() {
this.times++;
this.$emit('val-changed', 'Times changed ' + this.times);
}
}
});
var app = new Vue({
el: '#app',
data: {
title: 'Original Value',
}
})