Vue.js Prevent Page Refresh on Prompt Dialog Cancel
To prevent the page from refreshing when clicking "Cancel" in the prompt dialog, you can modify the showPrompt method as follows:
showPrompt(event) {
// Customize the prompt logic
event.preventDefault();
event.returnValue = \"\\"; // Compatibility with older browsers
this.$store.dispatch('staff/closeWebSocket');
// Prompt dialog
const confirmationMessage = \"Are you sure you want to leave this page?\\";
event.returnValue = confirmationMessage; // Display the prompt dialog
// Listen for the user's choice
const result = confirm(confirmationMessage);
if (result) {
// User clicked \"OK\", proceed with leaving the page
this.$store.dispatch('staff/closeWebSocket');
event.preventDefault();
} else {
// User clicked \"Cancel\", do not leave the page
event.returnValue = false;
}
},
This modified method displays a prompt dialog asking the user if they want to leave the page. If the user clicks "OK", the this.$store.dispatch('staff/closeWebSocket') action is dispatched and the page is left. If the user clicks "Cancel", the event.returnValue is set to false, preventing the page from refreshing.
原文地址: https://www.cveoy.top/t/topic/p68w 著作权归作者所有。请勿转载和采集!