Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Updating parent values in Vue.js 3 doesn't work for composite API

I’ve the following vue component (based on PrimeVue):

<template lang="pug">
Dialog(:visible="dShow" :modal="true" :draggable="false" header="My Dialog" :style="{ width: '50vw' }" @update:visible="hide()")
  p test
  template(#footer)
    Button(label="Test")
</template>

<script setup>
import { ref, watch, defineEmits } from 'vue'

const emit = defineEmits(['update:show'])

const props = defineProps({
  show: {
    type: Boolean,
    default: false
  }
})

const dShow = ref(false)

watch(() => props.show, (newValue) => {
  dShow.value = newValue
})

const hide = () => {
  emit('update:show', false)
}

</script>

My problem is when I click on the close icon of the dialog, the update:visible function (hide) is being called and the update:show event is getting emitted, but the dialog doesn’t disappear. It’s still show… so I assume the parent value isn’t updated correctly. Any idea what I’m doing wrong here?

I embed the component in the parent as following:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<template lang="pug">
Button(icon="icon pi pi-plus" @click="abcdef = true")
test(:show="abcdef")
</template>

<script setup>
import test from 'mydialog.vue'

const abcdef = ref(false)
</script>

>Solution :

Currently, the parent component sets the :show prop, but it does not react to changes. Use v-model for two-way binding, so that the value in the parent component can be updated from the child through update:show events:

test(v-model:show="abcdef")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading