Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vuetify: programmatically showing dialog

vuetify says: If you want to programmatically open or close the dialog, you can do so by using v-model with a boolean value.

However I am quite unclear on what this means. Saying "using v-model" is vague at best. The parent component knows on setup if it should open but I am unclear on how to dynamically change this in the child. Am i supposed to pass it using v-bind?

<login v-bind:showDialog></login>

If so how does the child component deal with this?

Vuetify Dialog info here: https://vuetifyjs.com/components/dialogs

like image 202
jpro Avatar asked Jun 08 '17 22:06

jpro


3 Answers

Дмитрий Алферьев answer's is correct but get "Avoid mutating a prop directly" warning, because when close dialog, v-dialog try change v-model to false, while we passed props to v-model and props value won't change. to prevent the warning we should use :value , @input

<template>
    <v-dialog :value="dialog" @input="$emit('update:dialog',false)" @keydown.esc="closeDialog()" >
    ...
    </v-dialog>
</template>
<script>
    export default {
        props: {
            dialog: Boolean
        },
        methods: {
            closeDialog(){
                this.$emit('closeDialog');
            }
        }

In parent

<template>
    <v-btn color="primary" @click="showDialog=true"></v-btn>
    <keep-alive>
        <my-dialog
            :dialog.sync="showEdit"
            @closeDialog="closeDialog"
        >
        </my-dialog>
    </keep-alive>
</template>
<script>
    data(){
        return {
            showEdit:false,
        },
    },
    methods: {
        closeDialog(){
            this.showEdit = false;
        },
    }

like image 179
Mohsen Avatar answered Nov 15 '22 11:11

Mohsen


As I understand you have a child component which have a dialog within it. Not sure that this is 100% right, but this is how I implement it. Child component with dialog:

<template>
  <v-dialog v-model="intDialogVisible">
...
</template>

<script>
...
export default {
props: {
    dialogVisible: Boolean,
    ...
},
computed: {
    intDialogVisible: {
      get: function () {
        if (this.dialogVisible) {
          // Some dialog initialization code could be placed here
          // because it is called only when this.dialogVisible changes
        }

        return this.dialogVisible
      },
      set: function (value) {
             if (!value) {
               this.$emit('close', some_payload)
             }
      }
}

in parent component we use it:

<my-dilaog :dialogVisible="myDialogVisible"
           @close="myDialogClose">
</my-dialog>

data () {
  return {
    myDialogVisible: false
  }
},
methods: {
  myDialogClose () {
    this.myDialogVisible = false
    // other code
  }
}
like image 30
Дмитрий Алферьев Avatar answered Nov 15 '22 10:11

Дмитрий Алферьев


v-model is a directive. You would use v-model, not v-bind.

The page you link has several examples. If you click on the <> button on the first one, it shows HTML source of

<v-dialog v-model="dialog">

v-model makes a two-way binding on a prop that is named value inside the component. When you set the bound variable's value to true, the dialog will display; when false, it will hide. Also, if the dialog is dismissed, it will set the variable's value to false.

like image 9
Roy J Avatar answered Nov 15 '22 11:11

Roy J