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 overwritte
var GuestMenu = Vue.extend({
props : {
uNpW:{type:Object}
}
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="uNpW.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="uNpW.password" class="form-control" id="Password" placeholder="Password">
</div>
</form>
</div>`,
});
App = new Vue ({
el: '#app',
data:
{
topMenuView: "guestmenu",
contentView: "guestcontent",
unAndPw:{username: "",password: ""}
}
})
in main html
<guest-menu :uNpW=unAndPw> </guest-menu>
you dont need emit or any other thing
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)
Follow this very easy instructions:
//Parent Component
<InsertForm :formSchema="formSchema" v-on:getFormSchema="setFormSchema"></InsertForm>
<script>
import InsertForm from "./insertForm.vue"; //select file
export default {
components: { InsertForm },
data: () => ({
formSchema:{
id:'',
webUrl:''
},
}),
methods: {
setFormSchema(data)
{
this.formSchema = data.formSchema;
}
}
}
</script>
// From Child Component. That means from insertForm.vue file
<script>
export default {
props: ["formSchema"],
data: () => ({
}),
}
//Where you need
this.$emit("getFormSchema", {'formSchema':{"id":1,"webUrl":"bdprescription.com"}});
</script>
Fast solution if your prop is an object.
You can avoid using $emit
or getting that error by using Object.assign()
in Javascript.
This is going to work the same as v-model
attribute.
example:
// Update the user
Object.assign(this.userProp, user);
A computed
property with appropriate get
and set
worked for me:
computed: {
dialogDataProp: {
get: function() {
return this.dialog;
},
set: function() {}
}
}
Code above for toggling a dialog box.