javascript - Vuetify dialog set prop mutation from child to parent - Stack Overflow

I'm currently opening a dialog ponent using thisParent<v-btn color="#EF5350" dark sma

I'm currently opening a dialog ponent using this

Parent

<v-btn color="#EF5350" dark small absolute top right fab 
 @click="showDialog">

   <v-icon>zoom_in</v-icon>  

</v-btn>

<UIDialog :dialog="dialog" @updateDialog="dialog = $event" />

<script>
  import UIDialog from '@/ponents/UI/UIDialog';
  export default {
    data() {
      return {
        dialog: false
      }
    }
    ponents: {
      UIDialog
    }, 
    methods: {
      showDialog() {
         this.dialog = true;
      }
    }
  }
</script>

This opens the dialog since I set dialog to true

Child

<v-dialog v-model="dialog" fullscreen scrollable>
  <v-card>
     This is a test
  </v-card>
</v-dialog>

<script>
 export default {
   props: {
     dialog: { type: Boolean, default: false }
   }, 
   watch: {
     dialog(val) {
       if (!val) this.$emit('updateDialog', false)
     }
   }
 }
</script>

I use watch since vue dialog doesn't have event. I managed to close the dialog but I'm still getting

Avoid mutating a prop directly since the value will be overwritten whenever the parent ponent re-renders. Instead, use a data or puted property based on the prop's value

I'm currently opening a dialog ponent using this

Parent

<v-btn color="#EF5350" dark small absolute top right fab 
 @click="showDialog">

   <v-icon>zoom_in</v-icon>  

</v-btn>

<UIDialog :dialog="dialog" @updateDialog="dialog = $event" />

<script>
  import UIDialog from '@/ponents/UI/UIDialog';
  export default {
    data() {
      return {
        dialog: false
      }
    }
    ponents: {
      UIDialog
    }, 
    methods: {
      showDialog() {
         this.dialog = true;
      }
    }
  }
</script>

This opens the dialog since I set dialog to true

Child

<v-dialog v-model="dialog" fullscreen scrollable>
  <v-card>
     This is a test
  </v-card>
</v-dialog>

<script>
 export default {
   props: {
     dialog: { type: Boolean, default: false }
   }, 
   watch: {
     dialog(val) {
       if (!val) this.$emit('updateDialog', false)
     }
   }
 }
</script>

I use watch since vue dialog doesn't have event. I managed to close the dialog but I'm still getting

Avoid mutating a prop directly since the value will be overwritten whenever the parent ponent re-renders. Instead, use a data or puted property based on the prop's value
Share Improve this question asked Jun 17, 2019 at 6:37 AllenCAllenC 2,7541 gold badge46 silver badges79 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 2

I managed to solve my problem by using puted property to get and set the dialog

Child

<v-dialog v-model="dialog" fullscreen scrollable>
  <v-card>
     This is a test
  </v-card>
</v-dialog>

<script>
 export default {
   props: {
     dialog: { type: Boolean, default: false }
   }, 
   puted: {
     dialogState: {
        get() { 
          return this.dialog;
        }, 
        set(val) {
          this.$emit('updateDialog', false);
        }
     }
   }
 }
</script>

This worked for me:

Parent

<app-my-dialog :dialog="doShowDialog" @close="doShowDialog = false"></app-my-dialog>

Child ponent (app-my-dialog)

<template>
  <v-dialog :value="showDialog" @click:outside="close()">
    <v-btn @click="close()">Close</v-btn>
  </v-dialog>
</template>
<script>
  export default {
    props: {
      dialog: {
        type: Boolean,
        default: false
     }
    }, 
    puted: {
      showDialog() {
        return this.dialog;
      }
    },
    methods: {
      close() {
        this.$emit('close')
      },
    }
  }
</script>

Note that if you use <v-dialog :persistent="true" ... (that means the dialog won't close on click outside itself), you can omit the @click:outside="close()"

Depose your v-model on UIDialog, in favor of

<v-dialog v-bind:value="dialog" v-on:input="emitOutput">

where emitOutput outputs a 'value' event

emitOutput(value) { this.$emit('input', value) }

-- This should handle the message of prop mutation in the console and also provide ponent level visibility control. You won't need to do any handling for custom events on the parent when emitting an 'input' event.

Use :value and @input instead of declaring prop to v-model

Prop mutation error happens because v-model have already :value and its mutator, which is triggers this error. If you will use simple :value and declare for @input method close() it will work

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745202707a4616422.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信