I'm currently working on a frontend with Vuetify 3 and I was thinking about the DRY principle several times now. But sometimes I'm having issues wrapping my head around Vue and Vuetify and therefore give up on DRY.
My current example is using a reusable v-dialog
for selecting an item out of a list. I want a button in the parent component to activate the child component, which is the dialog. If I select an item in the dialog, it will emit this item.
The problem I'm facing now is that I can't get the dialog to open in the first place.
Here is the child component CustomSelector
:
<template>
<v-dialog>
Some logic to displace the list and emit the selection
</v-dialog>
</template>
In the parent component I have a button, that toggles the v-model
for the child.
<template>
<div>
<v-btn @click="toggleCustomSelection">Select</v-btn>
<custom-selector v-model="showCustomSelector"></custom-selector>
</div>
</template>
<script setup>
// imports and stuff
const showCustomSelector = ref(false);
function toggleCustomSelection() {
showCustomSelector.value = !showCustomSelector.value;
}
</script>
If I don't implement the CustomSelector
as it's own component but directly put the actual dialog in the parent component, then it works. But as soon as I outsource it to it's own component, it doesn't work anymore.
How can I make this work? If this is not the usual way to do it, what is a better way?