Using vue.js to sync a prop between parent and child. The problem is sync uses events and every time I change the value I have to wait for $nextTick before the value updates. This is not ideal because I do not want to put $nextTick every time I change the value. Is there a way to make the event / prop update happen immediately?
HTML:
<div id="app">
<foo inline-template v-bind:bar.sync="bar">
<div>
<button v-on:click="handler_button_click">Set to 5</button>
</div>
</foo>
<span>bar: {{bar}}</span>
</div>
JS:
const EVENT_UPDATE_BAR = "update:bar";
Vue.component("foo", {
props:["bar"],
computed:{
_bar:{
get:function(){
return this.bar;
},
set:function(value){
//Mutating the prop here solves the problem, but then I get a warning about mutating the prop...
//this.bar = value;
this.$emit(EVENT_UPDATE_BAR, value);
}
}
},
methods:{
handler_button_click:function(){
//It seems that $nextTick must run before value is updated
this._bar = 5;
//This shows old value - event / prop has not fully propagated back down to child
alert("bar: " + this._bar);
}
}
});
new Vue({
el:"#app",
data:{
bar:1
}
});
See working example on CodePen: https://codepen.io/koga73/pen/MqLBXg