How do I warn a user of unsaved changes before leaving a page in Vue

前端 未结 3 1435
天命终不由人
天命终不由人 2021-02-02 09:28

I have an UnsavedChangesModal as a component that needs to be launched when the user tries to leave the page when he has unsaved changes in the input fields (I have

3条回答
  •  迷失自我
    2021-02-02 09:46

    These answers only cover navigation within Vue. If the user refreshes the page or navigates to a different site, that will not be caught. So you also need something like:

    window.onbeforeunload = () => (this.unsavedChanges ? true : null);
    

提交回复
热议问题