I have a component A,there is a dialog in this component A .I want to use visible
property to control the visible of dialog.
Component A:
<template>
<dialog :visible="visibleProp"/>
</templage>
<script setup>
const visibleProp= ref(true)
<script>
Dialog:
<template>
<dialog v-model="visible"></dialog>
</templage>
<script setup>
const prop = defineProps(['visible'])
<script>
note:dialog is another component.
I get this error
v-model cannot be used on a prop, because local prop bindings are not writable.Use a v-bind binding combined with a v-on listener that emits update:x event instead.
this is may caused by props should be considered readonly within the component.
How can I change my code to achieve my goal?Thanks a lot!
2
Answers
Vue3 SFC Playground
Use
v-model
correctly – update it with a dedicated event. You can’t change component props directly – they are read-only. In your case I suggest to name yourv-model
visibility
to make it more explicitly. Refer to the Vue’s documentation:https://vuejs.org/guide/components/v-model.html
If you use a HTML native dialog you should watch your prop and call methods on the dialog’s ref.
I strongly recommend to use the native HTML dialog due much better keyboard navigation and accessibility.
App.vue:
MyDialog.vue
Dialog