I've a component with a simple v-dialog to show a message to user and a v-btn to close it. The scenario is:
- User click on the button that show
v-dialog's component. - User click on the
v-btnto close the component - A error is triggered on console:
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: "show" - If click again on the button to open dialog, the dialog is not re-open, because the
data() shownot change the value from the component'sv-btn.
The dialog component BasicMessageDialog.vue
<template>
<div class="text-center">
<v-dialog v-if="showDialog" width="500">
<v-card>
<v-card-title primary-title class="title">Ops...</v-card-title>
<v-card-text class="body-1">{{message}}</v-card-text>
<v-card-actions>
<v-spacer></v-spacer>
<v-btn text color="primary" @click="show = false" class="body-1">Beleza!</v-btn>
</v-card-actions>
</v-card>
</v-dialog>
</div>
</template>
<script>
export default {
name: "BasicMessageDialog",
computed: {
showDialog() {
return this.show;
}
},
props: {
show: Boolean,
message: String
}
};
</script>
<style>
</style>
The main component Login.vue
<template>
...
<BasicMessageDialog :message="messageBasicDialog" :show="showBasicMessageDialog">
...
</BasicMessageDialog>
</template>
<script>
import BasicMessageDialog from "@/components/BasicMessageDialog";
export default {
name: "Login",
components: {
BasicMessageDialog
},
data: () => ({
showBasicMessageDialog: false,
messageBasicDialog: "",
)},
methods: {
forgetPassword() {
console.log("forgetPassword");
if (this.email == "") {
this.messageBasicDialog = "Digite o e-mail no campo!";
this.showBasicMessageDialog = true;
}
}
}
</script>