I create a custom lightning component and have some unsaved data. I'd like to show a warning and optionally prevent the navigation when clicking to a navigation bar (such as Home, Account,.. ).
I already tried with aura:locationChange and beforeunload, but it didn't work. Does anyone have any solution for this case?
<aura:handler event="aura:locationChange" action="{!c.handleLocationChange}"/>
handleLocationChange : function (cmp, event, helper) {
confirm('You have unsaved data. Do you want to leave?');
event.preventDefault();
}
window.addEventListener('beforeunload', function (e) {
confirm('You have unsaved data. Do you want to leave?');
e.preventDefault();
}
lightning:unsavedChanges
here.ligthning:unsavedChanges
will work fine.