I have very common problem with upgrading to Vue 2.0
I am getting warning:
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: "username" (found in the component )
I read the documentation many times but I still can't understand how to fix it.
username
and password
are declared in the main Vue app.
Here is my code:
var GuestMenu = Vue.extend({ props : ['username', 'password'], template: ` <div id="auth"> <form class="form-inline pull-right"> <div class="form-group"> <label class="sr-only" for="UserName">User name</label> <input type="username" v-model="username" class="form-control" id="UserName" placeholder="username"> </div> <div class="form-group"> <label class="sr-only" for="Password">Password</label> <input type="password" v-model="password" class="form-control" id="Password" placeholder="Password"> </div> </form> </div>`, });
App = new Vue ({ el: '#app', data: { topMenuView: "guestmenu", contentView: "guestcontent", username: "", password: "", } })
I tried v-bind
but it does not seem to work, and I can't understand why. It should bind the value to parent (the main Vue app)
Here is the error message in full: Error message: 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.
The answer is simple, you should break the direct prop mutation by assigning the value to some local component variables(could be data property, computed with getters, setters, or watchers). Here's a simple solution using the watcher. It's what I use to create any data input components and it works just fine.
Short answer is: NO. Long answer is: It will not work.
From Vue 2.3.0 on you can use the .sync
modifier:
Sample from https://vuejs.org/v2/guide/components-custom-events.html#sync-Modifier:
<text-document :title.sync="title"></text-document>
and in your controller...
this.$emit('update:title', newTitle)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With