<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Pawel</title>
    <description>The latest articles on DEV Community by Pawel (@algorytmus).</description>
    <link>https://dev.to/algorytmus</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1366848%2F0790979f-907a-4fe7-b33d-23f7e2914886.png</url>
      <title>DEV Community: Pawel</title>
      <link>https://dev.to/algorytmus</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/algorytmus"/>
    <language>en</language>
    <item>
      <title>Robot framework</title>
      <dc:creator>Pawel</dc:creator>
      <pubDate>Tue, 19 Mar 2024 22:11:13 +0000</pubDate>
      <link>https://dev.to/algorytmus/robot-framework-41l7</link>
      <guid>https://dev.to/algorytmus/robot-framework-41l7</guid>
      <description>&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
Aby w Vue 3 poinformować użytkownika, że może utracić niezapisane dane przy opuszczaniu strony, musisz użyć dwóch mechanizmów:

✅ 1. beforeunload – gdy użytkownik opuszcza stronę (np. zamyka kartę, przeładowuje)
ts
Copy
Edit
onBeforeMount(() =&amp;gt; {
  window.addEventListener('beforeunload', handleBeforeUnload)
})

onBeforeUnmount(() =&amp;gt; {
  window.removeEventListener('beforeunload', handleBeforeUnload)
})

function handleBeforeUnload(e: BeforeUnloadEvent) {
  if (hasUnsavedChanges.value) {
    e.preventDefault()
    e.returnValue = '' // potrzebne, aby Chrome wyświetlił alert
  }
}
🔐 Uwaga: Przeglądarki nie pozwalają już na niestandardowe komunikaty – pojawia się tylko ogólny alert systemowy.

✅ 2. beforeRouteLeave – gdy użytkownik zmienia trasę w aplikacji SPA (Vue Router)
ts
Copy
Edit
import { onBeforeRouteLeave } from 'vue-router'

onBeforeRouteLeave((to, from, next) =&amp;gt; {
  if (hasUnsavedChanges.value) {
    const confirmed = window.confirm('Masz niezapisane zmiany. Czy na pewno chcesz opuścić stronę?')
    if (confirmed) {
      next()
    } else {
      next(false) // zatrzymaj nawigację
    }
  } else {
    next()
  }
})
📦 Przykład: komponent FormView.vue
vue
Copy
Edit
&amp;lt;script setup lang="ts"&amp;gt;
import { ref, onBeforeMount, onBeforeUnmount } from 'vue'
import { onBeforeRouteLeave } from 'vue-router'

const hasUnsavedChanges = ref(false)

function handleBeforeUnload(e: BeforeUnloadEvent) {
  if (hasUnsavedChanges.value) {
    e.preventDefault()
    e.returnValue = ''
  }
}

onBeforeMount(() =&amp;gt; {
  window.addEventListener('beforeunload', handleBeforeUnload)
})

onBeforeUnmount(() =&amp;gt; {
  window.removeEventListener('beforeunload', handleBeforeUnload)
})

onBeforeRouteLeave((to, from, next) =&amp;gt; {
  if (hasUnsavedChanges.value) {
    const confirmed = window.confirm('Masz niezapisane zmiany. Czy na pewno opuścić stronę?')
    next(confirmed)
  } else {
    next()
  }
})
&amp;lt;/script&amp;gt;
✅ Tip: Kiedy ustawić hasUnsavedChanges = true?
Np. w v-model, @input, lub po kliknięciu „Edytuj”.

Chcesz wersję z PrimeVue ConfirmDialog zamiast window.confirm()?

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>java</category>
    </item>
  </channel>
</rss>
