【发布时间】:2021-10-26 08:01:09
【问题描述】:
我试图在离开页面之前提示用户。我注意到当用户从元素链接进入页面然后当我点击返回时,触发 beforeunload 事件。
但是,当用户通过 NextJs Link 组件进入页面时,点击返回不会触发 beforeunload 事件。
如何在使用组件时仍然提示用户?
例如,通过此链接进入页面并单击返回不会在卸载前触发
<Link href={`/event/${encodeURIComponent(event.slug)}`}>
<a>
<h3>{event.name}</h3>
</a>
</Link>
但是常规的元素链接会在点击返回时触发 beforeunload
例子
<a href={"event/" + event.slug}>
<h3>{event.name}</h3>
</a>
【问题讨论】:
-
像Want to have an event handler for the browser's back button with next.js 这样的解决方案是否适用于您的用例?它使用
next/router的beforePopState来监听路由变化。
标签: javascript reactjs next.js