I am new to vuejs and I am trying to sync active data to parent but getting an error as
vue.js:523 [Vue warn]: Avoid mutating a prop directly since the value will be overwritten whenever the parent component re-renders. Instead, use a data or computed property based on the prop's value. Prop being mutated: "active" (found in component )
My Vuejs Code is as below
<div id="app">
<pre>
{{$data}}
</pre>
<div v-for="plan in plans">
<plan :plan="plan" :active.sync="active"></plan>
</div>
</div>
<template id="mytemplate">
<div>
{{$data}}
<span>{{plan.name}}</span>
<span>{{plan.price}}</span>
<button @click="setActivePlan">upgrade</button>
</div>
</template>
<script src="vue.js"></script>
<script>
new Vue({
el: "#app",
data: {
active:this.active,
plans: [
{name: 'Diamond', price: '1000'},
{name: 'Gold', price: '500'},
{name: 'Silver', price: '250'},
{name: 'Free', price: '0'}
]
},
components: {
plan: {
template: "#mytemplate",
props: ['plan', 'active'],
methods: {
setActivePlan: function () {
this.active = this.plan
}
}
}
}
});
</script>
Can any one help me to solve this
As the error suggests, you are trying to change one of the props
active
.As props are dynamically sent from parent, they will change whenever parent changes those, if you change them in child also, there will be conflict, thats why you are getting this error.
As par the documentation:
So the proper way to do this will be to emit an event, which will call a method in parent and change the variable
active
in parent where it is defined. Following will be the code changes:I hope this is the version problem. please show your VUEJS version, else get the version from below code.