Title rewritten to: PrimeVue ConfirmDialog Multiple Instances Issue
P粉328911308
P粉328911308 2024-01-05 20:57:59
0
1
440

I have a component from PrimeVue and it works fine except it opens multiple times when activated; for reference, I do that multiple times throughout the process Components, some confirmation dialogs only open once, most usually open twice. When the dialog is accepted or rejected, they all close immediately, however, when pressing the "X" in the upper right corner of the dialog, it only closes one instance at a time, showing multiple dialogs open.

What I tried: Use key

<ConfirmDialog key="myDialog" />

...

const confirmer = (
 message,
 header,
 icon,
 ) => {
confirm.require({
 accept: () => { confirm.close()},
 reject: () => { confirm.close()},
 key: 'myDialog'
})}

thanks for your help.

P粉328911308
P粉328911308

reply all(1)
P粉021708275

I encountered this problem and I found that it was caused by declaring multiple ConfirmDialog components in the DOM markup. For example, if you add a confirmation dialog to every component that uses it, and you happen to have more than 2 components loading on the page at the same time, you will see 1 for each The dialog box exists on this page.

The solution is to declare ConfirmDialog only once in the root Vue component, and then each time it is called, just import the useConfirm function and then use that function to call the dialog.

For example:

Application View

<script setup>
import ConfirmDialog from 'primevue/confirmdialog';
</script>

<template>
    <router-view></router-view>

    <ConfirmDialog />
</template>

All other components:

<script setup>
import { useConfirm } from 'primevue/useconfirm';

const confirm = useConfirm();

const handleRemoveThing = () => {
    // bye
};

const onRemoveThing = () => {
    confirm.require({
        message: 'Are you sure you want to remove some thing?',
        header: 'Remove Thing',
        icon: 'icon-delete',
        rejectLabel: 'Cancel',
        acceptLabel: 'Remove',
        acceptClass: 'p-button-danger',
        accept: handleRemoveThing,
    });
};
</script>

<template>
    <div>
        <button @click="onRemoveThing">Remove</button>
    </div>
</template>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template